forked from raylu/sbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.py
41 lines (34 loc) · 1.3 KB
/
reddit.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
import requests
import config
rs = requests.Session()
rs.headers['User-Agent'] = 'python:sbot:v0 (by /u/raylu)'
def headpat(cmd):
items = _reddit_request('/r/headpats/random')
item = items[0]['data']['children'][0]['data']
resolutions = item['preview']['images'][0]['resolutions']
image = resolutions[1]
image_url = image['url'].replace('&', '&')
embed = {
'title': item['title'],
'url': 'https://www.reddit.com/' + item['permalink'],
'image': {'url': image_url, 'width': image['width'], 'height': image['height']},
}
cmd.reply('', embed)
def _reddit_request(path):
url = 'https://oauth.reddit.com' + path
access_token = config.state.reddit_access_token
if access_token is not None:
r = rs.get(url, headers={'Authorization': 'bearer ' + access_token})
if access_token is None or r.status_code == 401:
_refresh_access_token()
r = rs.get(url, headers={'Authorization': 'bearer ' + config.state.reddit_access_token})
r.raise_for_status()
return r.json()
def _refresh_access_token():
r = rs.post('https://www.reddit.com/api/v1/access_token',
auth=(config.bot.reddit['api_id'], config.bot.reddit['api_secret']),
data={'grant_type': 'client_credentials'})
r.raise_for_status()
access_token = r.json()['access_token']
config.state.reddit_access_token = access_token
config.state.save()