-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsr.py
81 lines (74 loc) · 2.61 KB
/
psr.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
import requests
import json
import time
import os
sub = 'photoshoprequest'
timer = 60
free = False
thumb = True
def get_post():
try:
base_url = f'https://www.reddit.com/r/'+sub+'/new.json?count=1&t=all'
request = requests.get(base_url, headers = {'User-agent': 'yourbot'})
return request.json()
except:
return
def download_pic(new_post):
try:
img = new_post['data']['children'][0]['data']['url_overridden_by_dest']
imgname = os.path.basename(img)
if imgname[-4]=='.' or imgname[-4]=='w':
img_data = requests.get(img).content
with open('/home/ashu/shop/'+imgname, 'wb') as handler:
handler.write(img_data)
return
except:
return
def download_gallery(new_post):
for i in range(5):
try:
img = list(new_post['data']['children'][0]['data']['media_metadata'].values())[i]['s']['u'].replace('amp;','')
simg = img[0:img.find('?')]
imgname = os.path.basename(simg)
if imgname[-4]=='.' or imgname[-4]=='w':
img_data = requests.get(img).content
with open('/home/ashu/shop/'+imgname, 'wb') as handler:
handler.write(img_data)
except:
break
return
def download_thumb(new_post):
try:
turl = new_post['data']['children'][0]['data']['thumbnail']
tdata = requests.get(turl).content
with open('/home/ashu/.scripts/thumb.jpg', 'wb') as handler:
handler.write(tdata)
return
except:
return
def download(new_post):
if 'media_metadata' in new_post['data']['children'][0]['data']:
download_gallery(new_post)
else:
download_pic(new_post)
return
while True:
new_post = get_post()
title = new_post['data']['children'][0]['data']['title'].replace("'","")
fi = '/home/ashu/.scripts/last'
with open(fi,'r') as f: tit = f.readline()
if tit != title:
if thumb==True: download_thumb(new_post)
flair = new_post['data']['children'][0]['data']['link_flair_css_class']
if flair=='paid' or title.lower().find('pay')!=-1 or title.lower().find('tip')!=-1 or title.find('$')!=-1:
cmd = 'notify-send -a "psr" -u critical '+"' '"+" '"+title+"'"
download(new_post)
else:
cmd = 'notify-send -a "psr" '+"' '"+" '"+title+"'"
if free==True: download(new_post)
if thumb==True: os.system(cmd+' -i "/home/ashu/.scripts/thumb.jpg"')
else: os.system(cmd)
f = open(fi,'w')
f.write(title)
f.close()
time.sleep(timer)