-
Notifications
You must be signed in to change notification settings - Fork 3
/
csvmaker1.py
executable file
·165 lines (126 loc) · 9.36 KB
/
csvmaker1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Nov 4 16:06:38 2020
@author: william
"""
# -*- coding: utf-8 -*-
"""api-for-real-practice.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19PkmzA0njoH9OWfBzoe7sS2NH0PVonFU
"""
import requests
import pandas as pd
import plotly.express as px
import random
def get_new_authentication():
base_url = 'https://www.reddit.com/'
data = {'grant_type': 'password', 'username': 'whammmond', 'password': '#InsertPasswordBeforeRunning'}
auth = requests.auth.HTTPBasicAuth('h6wy01n0-8H86A', 'gezsDAlid5PVXPt6ug_nyqO2XKg2Lw')
r = requests.post(base_url + 'api/v1/access_token',
data=data,
headers={'user-agent': 'MacOS:reddit-visualizer:v1.0 (by /u/whammmond)'},
auth=auth)
d = r.json()
print(d)
token = 'bearer ' + d['access_token']
return token
def request_new_posts(subreddit,token,after):
base_url = 'https://oauth.reddit.com'
headers = {'Authorization': token, 'User-Agent': 'MacOS:reddit-visualizer:v1.0 (by /u/whammmond)'}
payload = {'limit' : '100', 'show':'all','t':'year','after':f'{after}'}
response = requests.get(base_url + f'/r/{subreddit}/top', headers=headers, params=payload)
if response.status_code == 200:
response_json = response.json()
return response_json
def add_post_data(subreddit, response):
posts = response['data']['children']
post_data = []
corona_keys = ['corona', 'covid', '-19', 'pandemic']
trump_keys = ['trump', 'donald']
biden_keys = ['biden', 'joseph r']
racial_justice_keys = ['george floyd', 'black lives','blacklives','blm','racial justice','defund the police','police reform']
economy_keys = ['the fed','stimulus','economy','unemployment','small business','federal reserve','economic']
for i in range(len(posts)):
post_title = posts[i]['data']['title'].casefold()
if any(c in post_title for c in corona_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic':'COVID-19','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),'Upvote Ratio':posts[i]['data']['upvote_ratio']})
if any(c in post_title for c in trump_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Donald Trump','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),'Upvote Ratio':posts[i]['data']['upvote_ratio']})
if any(c in post_title for c in biden_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Joe Biden','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),'Upvote Ratio':posts[i]['data']['upvote_ratio']})
if any(c in post_title for c in racial_justice_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Racial Justice Protests','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),'Upvote Ratio':posts[i]['data']['upvote_ratio']})
if any(c in post_title for c in economy_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'The Economy','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),'Upvote Ratio':posts[i]['data']['upvote_ratio']})
return post_data
def add_post_data_two(subreddit, response):
posts = response['data']['children']
post_data = []
total_upvotes = 0
total_comments = 0
total_downvotes = 0
corona_keys = ['corona', 'covid', '-19', 'pandemic']
trump_keys = ['trump', 'donald']
biden_keys = ['biden', 'joseph r']
racial_justice_keys = ['george floyd', 'black lives','blacklives','blm','racial justice','defund the police','police reform']
economy_keys = ['the fed','stimulus','economy','unemployment','small business','federal reserve','economic']
for i in range(len(posts)):
post_title = posts[i]['data']['title'].casefold()
if any(c in post_title for c in corona_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic':'COVID-19','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],
'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']), 'Upvote Ratio':posts[i]['data']['upvote_ratio'],
'Downvotes':int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])})
total_upvotes += posts[i]['data']['ups']
total_comments += posts[i]['data']['num_comments']
total_downvotes += int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])
if any(c in post_title for c in trump_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Donald Trump','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],
'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),
'Upvote Ratio':posts[i]['data']['upvote_ratio'],'Downvotes':int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])})
total_upvotes += posts[i]['data']['ups']
total_comments += posts[i]['data']['num_comments']
total_downvotes += int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])
if any(c in post_title for c in biden_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Joe Biden','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],
'Upvotes':posts[i]['data']['ups'],'Upvote Ratio':posts[i]['data']['upvote_ratio'],
'Downvotes':int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])})
total_upvotes += posts[i]['data']['ups']
total_comments += posts[i]['data']['num_comments']
total_downvotes += int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])
if any(c in post_title for c in racial_justice_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'Racial Justice Protests','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],
'Upvotes':posts[i]['data']['ups'],'Comments/Score Ratio':(posts[i]['data']['num_comments'])/(posts[i]['data']['ups']),
'Upvote Ratio':posts[i]['data']['upvote_ratio'],'Downvotes':int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])})
total_upvotes += posts[i]['data']['ups']
total_comments += posts[i]['data']['num_comments']
total_downvotes += int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])
if any(c in post_title for c in economy_keys):
post_data.append({'Subreddit':f'{subreddit}','Topic': 'The Economy','Title':posts[i]['data']['title'],'Comments':posts[i]['data']['num_comments'],
'Upvotes':posts[i]['data']['ups'],'Upvote Ratio':posts[i]['data']['upvote_ratio'],
'Downvotes':int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])})
total_upvotes += posts[i]['data']['ups']
total_comments += posts[i]['data']['num_comments']
total_downvotes += int((posts[i]['data']['ups']/(posts[i]['data']['upvote_ratio']+random.uniform(-0.005, 0.005)))-posts[i]['data']['ups'])
for x in post_data:
x['Relative Upvotes'] = x['Upvotes']/total_upvotes
x['Relative Comments'] = x['Comments']/total_comments
x['Relative Downvotes'] = x['Downvotes']/total_downvotes
return post_data
big_list = []
auth = get_new_authentication()
response = request_new_posts('news',f'{auth}','None')
big_list.extend(add_post_data_two('news',response))
while(response['data']['after']!=None):
response = request_new_posts('news',f'{auth}',response['data']['after'])
print(response['data']['after'])
big_list.extend(add_post_data_two('news',response))
response = request_new_posts('politics',f'{auth}','None')
big_list.extend(add_post_data_two('news',response))
while(response['data']['after']!=None):
response = request_new_posts('politics',f'{auth}',response['data']['after'])
print(response['data']['after'])
big_list.extend(add_post_data_two('politics',response))
df = pd.DataFrame(big_list)
df.to_csv('/Users/william/Desktop/Python Website/output4.csv')