import requests
import requests.auth
client_auth = requests.auth.HTTPBasicAuth('foo', 'bar')
post_data = {"grant_type": "password", "username": "user", "password": "pass"}
headers = {"User-Agent": "RedditUser"}
authentication = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=post_data, headers=headers)
token = "bearer "+authentication.json()['access_token']
headers = {"Authorization": token, "User-Agent": "user"}
params = {"t": "day"}
response = requests.get("https://oauth.reddit.com/r/python/top", headers=headers, params=params)
python_top = response.json()
children = python_top['data']['children']
python_data=[]
for element in children:
python_data.append(element['data'])
#print(python_data)
python_top_articles = []
most_upvoted_votes = 0
for element in python_data:
python_top_articles.append(element['title'])
if(element['ups']>most_upvoted_votes):
most_upvoted_votes=element['ups']
most_upvoted=element['id']
print(most_upvoted)
response = requests.get('https://oauth.reddit.com/r/python/comments/9o3x81', headers=headers)
comments = response.json()
comments_list = comments[1]['data']['children']
#print(comments_list)
most_voted = 0
for element in comments_list:
data = element['data']
if(data['ups']>most_voted):
most_voted = data['ups']
most_upvoted_comment = data #['id']
print(most_upvoted_comment['body'])
payload = {"dir": 1, "id": "d16y4ry"}
headers = {"Authorization": token, "User-Agent": "RedditCDMal"}
response = requests.post('https://oauth.reddit.com/api/vote',headers=headers,json=payload)
print(response.status_code)