-
Notifications
You must be signed in to change notification settings - Fork 15
/
dmbot.py
96 lines (72 loc) · 2.68 KB
/
dmbot.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
import urllib
import praw
import os
import requests
import time
import json
from selenium import webdriver
from PIL import Image
def getScreenShot(url):
# http://stackoverflow.com/a/15870708
driver.get(url)
element = driver.find_element_by_id('js-article-text')
comment = element.find_element_by_id('socialLinks')
end = comment.location
location = element.location
size = element.size
driver.save_screenshot('screenshot.jpg')
im = Image.open('screenshot.jpg')
left = location['x']
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + end['y'] - 510
im = im.crop((left, top, right, bottom))
im.save('screenshot.jpg', "JPEG")
driver = webdriver.PhantomJS()
comment = """
[Non-Daily Mail Mirror](%s)
^^Code ^^on ^^[github](https://github.com/bag-man/dm_bot).
"""
postedOn = []
r = praw.Reddit(user_agent='DM_Mirror')
r.login('DailMail_Bot', 'asdf1234')
reddits = {'reddevils', 'politic', 'dailymail'}
print "Logged in"
first = True
while True:
try:
posts = r.get_domain_listing('dailymail.co.uk', sort='new', limit=10)
for submission in posts:
if str(submission.subreddit).lower() not in reddits:
if first is True:
postedOn.append(submission.id)
if submission.id not in postedOn:
print "We got one! " + submission.short_link
if submission.domain == "dailymail.co.uk":
try:
getScreenShot(submission.url.rstrip())
except Exception, e:
print "Failed to get image at:" + submission.url.rstrip()
print e
elif submission.domain == "i.dailymail.co.uk":
urllib.urlretrieve(submission.url, "screenshot.jpg")
try:
res = requests.post(
url="https://filebunker.pw/upload.php",
files={"files[]": open("screenshot.jpg", "rb")}
)
data = json.loads(res._content)
link = data["files"][0]["url"]
submission.add_comment(comment % (link))
print "Posted!"
except Exception, e:
print "Failed to submit:"
print e
if(os.path.isfile('screenshot.jpg')):
os.remove('screenshot.jpg')
postedOn.append(submission.id)
time.sleep(5)
first = False
except Exception, e:
print e
driver.quit()