├── README.md ├── banned.py ├── dmbot.py ├── requirements.txt ├── sunbot.py └── test.py /README.md: -------------------------------------------------------------------------------- 1 | dm_bot 2 | ====== 3 | 4 | ** This bot has been decomissioned ** 5 | 6 | This bot is more of a learning exercise than anything else, no harm intended. 7 | 8 | It works by using phantom.js and selenium to grab a screenshot of the whole page and mark some points for reference. The next step is to crop just the article out of the page by using pillow, and then save it as a jpg to save bandwidth. Once it has done that it can post a comment on the reddit post with a link to the image. 9 | 10 | Needs all this installed to work: 11 | 12 | * praw - get reddit posts and comment on them 13 | * selenium - get screenshot through phantom.js 14 | * pillow - crop screenshot to just the article 15 | * libjpeg8 - allows pillow to save as jpeg 16 | * phantom.js - renders the screenshot 17 | 18 | ![A picture of the output](http://i.imgur.com/gFrtGnb.png) 19 | 20 | [Link to /u/DailMail_Bot. See him in action!](http://www.reddit.com/user/DailMail_Bot) 21 | 22 | I was originally using imgur, however imgur would compress images over 1MB so [they were unreadable.](https://i.imgur.com/CQ5tLg1.jpg) I now use [a.pomf.se](http://a.pomf.se/sodjxu.jpg) as it allows for the full image to be shown. 23 | 24 | I am aware the code is not in good shape, I keep just patching it manually to keep up with new image hosts, it is just intended to be a fun script, not a work of art! 25 | 26 | I have been banned from: 27 | * /r/sydney 28 | * /r/texas 29 | * /r/imagesofthe1970s 30 | * /r/ImagesOfThe2010s 31 | * /r/ImagesOfEngland 32 | * /r/DoctorWhumour 33 | * /r/historyblogs 34 | * /r/nufcirclejerk 35 | * /r/nufcirclejerk 36 | * /r/The_Donald 37 | * /r/Mr_Trump 38 | * /r/Minecraft 39 | * /r/ireland 40 | * /r/youranonnews 41 | * /r/army 42 | * /r/Conservatives_R_Us 43 | * /r/conservatives 44 | * /r/skeptic 45 | * /r/hipsterhuskies 46 | * /r/science 47 | * /r/SandersForPresident 48 | * /r/politota 49 | * /r/UKIP 50 | * /r/ukipparty 51 | * /r/gaming 52 | * /r/Romania 53 | * /r/worldnews 54 | * /r/photoshopbattles 55 | * /r/unitedkingdom 56 | * /r/funfacts 57 | * /r/travel 58 | * /r/China 59 | * /r/interestingasfuck 60 | * /r/conspiratard 61 | * /r/inthenews 62 | * /r/funny 63 | * /r/politics 64 | * /r/NaziHunting 65 | * /r/Celebs 66 | * /r/Conservative 67 | * /r/UpliftingNews 68 | * /r/WTF 69 | * /r/news 70 | * /r/AnyMore 71 | * /r/necrodancer 72 | * /r/celebnsfw 73 | * /r/space 74 | * /r/reactiongifs 75 | * /r/nottheonion 76 | * /r/ukpolitics 77 | * /r/WhiteRights 78 | * /r/unitedkingdom 79 | * /r/european 80 | * /r/ebola 81 | * /r/soccer 82 | 83 | [List of potential image hosts](https://docs.google.com/spreadsheets/d/1kh1TZdtyX7UlRd55OBxf7DB-JGj2rsfWckI0FPQRYhE/edit#gid=0) 84 | -------------------------------------------------------------------------------- /banned.py: -------------------------------------------------------------------------------- 1 | #!/bin/python2.7 2 | import praw 3 | 4 | r = praw.Reddit(user_agent='saved_links') 5 | r.login('DailMail_Bot', 'redacted', disable_warning=True) 6 | 7 | print "I have been banned from: " 8 | for message in r.get_inbox(limit=None, time='all'): 9 | if "banned" in message.body: 10 | print " * /r/" + str(message.subreddit) 11 | -------------------------------------------------------------------------------- /dmbot.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import praw 3 | import os 4 | import requests 5 | import time 6 | import json 7 | 8 | from selenium import webdriver 9 | from PIL import Image 10 | 11 | 12 | def getScreenShot(url): 13 | # http://stackoverflow.com/a/15870708 14 | 15 | driver.get(url) 16 | 17 | element = driver.find_element_by_id('js-article-text') 18 | comment = element.find_element_by_id('socialLinks') 19 | 20 | end = comment.location 21 | location = element.location 22 | size = element.size 23 | 24 | driver.save_screenshot('screenshot.jpg') 25 | 26 | im = Image.open('screenshot.jpg') 27 | 28 | left = location['x'] 29 | top = location['y'] 30 | right = location['x'] + size['width'] 31 | bottom = location['y'] + end['y'] - 510 32 | 33 | im = im.crop((left, top, right, bottom)) 34 | im.save('screenshot.jpg', "JPEG") 35 | 36 | 37 | driver = webdriver.PhantomJS() 38 | 39 | comment = """ 40 | [Non-Daily Mail Mirror](%s) 41 | 42 | ^^Code ^^on ^^[github](https://github.com/bag-man/dm_bot). 43 | """ 44 | 45 | postedOn = [] 46 | r = praw.Reddit(user_agent='DM_Mirror') 47 | 48 | r.login('DailMail_Bot', 'asdf1234') 49 | 50 | reddits = {'reddevils', 'politic', 'dailymail'} 51 | 52 | print "Logged in" 53 | first = True 54 | 55 | while True: 56 | try: 57 | posts = r.get_domain_listing('dailymail.co.uk', sort='new', limit=10) 58 | for submission in posts: 59 | if str(submission.subreddit).lower() not in reddits: 60 | if first is True: 61 | postedOn.append(submission.id) 62 | 63 | if submission.id not in postedOn: 64 | print "We got one! " + submission.short_link 65 | if submission.domain == "dailymail.co.uk": 66 | try: 67 | getScreenShot(submission.url.rstrip()) 68 | except Exception, e: 69 | print "Failed to get image at:" + submission.url.rstrip() 70 | print e 71 | elif submission.domain == "i.dailymail.co.uk": 72 | urllib.urlretrieve(submission.url, "screenshot.jpg") 73 | 74 | 75 | try: 76 | res = requests.post( 77 | url="https://filebunker.pw/upload.php", 78 | files={"files[]": open("screenshot.jpg", "rb")} 79 | ) 80 | data = json.loads(res._content) 81 | link = data["files"][0]["url"] 82 | submission.add_comment(comment % (link)) 83 | print "Posted!" 84 | except Exception, e: 85 | print "Failed to submit:" 86 | print e 87 | 88 | if(os.path.isfile('screenshot.jpg')): 89 | os.remove('screenshot.jpg') 90 | postedOn.append(submission.id) 91 | time.sleep(5) 92 | first = False 93 | except Exception, e: 94 | print e 95 | 96 | driver.quit() 97 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | praw 2 | selenium 3 | image 4 | -------------------------------------------------------------------------------- /sunbot.py: -------------------------------------------------------------------------------- 1 | import urllib 2 | import praw 3 | import os 4 | import requests 5 | import time 6 | import json 7 | 8 | from selenium import webdriver 9 | from PIL import Image 10 | 11 | 12 | def getScreenShot(url): 13 | # http://stackoverflow.com/a/15870708 14 | 15 | driver.set_window_size(1280, 1024) 16 | driver.get(url) 17 | 18 | element = driver.find_element_by_class_name('article') 19 | comment = driver.find_element_by_css_selector('.tags--article') 20 | 21 | end = comment.location 22 | location = element.location 23 | size = element.size 24 | 25 | driver.save_screenshot('sunscreenshot.jpg') 26 | 27 | im = Image.open('sunscreenshot.jpg') 28 | 29 | left = location['x'] 30 | top = location['y'] 31 | right = location['x'] + size['width'] 32 | bottom = location['y'] + end['y'] - 365 33 | 34 | im = im.crop((left, top, right, bottom)) 35 | im.save('sunscreenshot.jpg', "JPEG") 36 | 37 | 38 | driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) 39 | 40 | comment = """ 41 | [Non-Sun Mirror](%s) 42 | 43 | ^^Code ^^on ^^[github](https://github.com/bag-man/dm_bot). 44 | """ 45 | 46 | postedOn = [] 47 | r = praw.Reddit(user_agent='DM_Mirror') 48 | 49 | r.login('DailMail_Bot', 'asdf1234') 50 | 51 | reddits = {'reddevils', 'politic', 'dailymail'} 52 | 53 | print "Logged in" 54 | first = True 55 | 56 | while True: 57 | try: 58 | posts = r.get_domain_listing('thesun.co.uk', sort='new', limit=10) 59 | for submission in posts: 60 | if str(submission.subreddit).lower() not in reddits: 61 | if first is True: 62 | postedOn.append(submission.id) 63 | 64 | if submission.id not in postedOn: 65 | print "We got one! " + submission.short_link 66 | if submission.domain == "thesun.co.uk": 67 | try: 68 | getScreenShot(submission.url.rstrip()) 69 | except Exception, e: 70 | print "Failed to get image at:" + submission.url.rstrip() 71 | print e 72 | 73 | try: 74 | res = requests.post( 75 | url="https://filebunker.pw/upload.php", 76 | files={"files[]": open("sunscreenshot.jpg", "rb")} 77 | ) 78 | data = json.loads(res._content) 79 | link = data["files"][0]["url"] 80 | submission.add_comment(comment % (link)) 81 | print "Posted!" 82 | except Exception, e: 83 | print "Failed to submit:" 84 | print e 85 | 86 | if(os.path.isfile('sunscreenshot.jpg')): 87 | os.remove('sunscreenshot.jpg') 88 | postedOn.append(submission.id) 89 | time.sleep(5) 90 | first = False 91 | except Exception, e: 92 | print e 93 | 94 | driver.quit() 95 | -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import json 3 | 4 | res = requests.post( 5 | url="https://filebunker.pw/upload.php", 6 | files={"files[]": open("screenshot.jpg", "rb")} 7 | ) 8 | print res._content 9 | 10 | data = json.loads(res._content) 11 | print data["files"][0]["url"] 12 | --------------------------------------------------------------------------------