Last active
August 29, 2015 14:16
-
-
Save Spittie/eef0c89cb5e4fe976e47 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from __future__ import print_function | |
import praw | |
import re | |
import requests | |
import time | |
import logging | |
from contextlib import contextmanager | |
REDDIT_USERNAME = 'calcolatore' | |
REDDIT_PASSWORD = 'nicetry' | |
USER_AGENT = ("Rita 0.3 for /r/italy by /u/nofunallowed98765") | |
SITES = [ | |
re.compile('.+corriere\.it.*'), | |
re.compile('.+beppegrillo\.it.*'), | |
re.compile('.+ilmanifesto\.info.*'), | |
re.compile('.+ansa\.it.*') | |
] | |
SUBREDDIT = 'italy' | |
POST_LIMIT = 10 | |
REFRESH_TIME = 600 | |
MESSAGE = '''Per chi naviga da cellulare: [archive.today]({url}) | [text-only](http://textise.net/showText.aspx?strURL={post_url}) | |
^^[Sorgente](https://gist.github.com/Spittie/eef0c89cb5e4fe976e47) ^^| ^^Ciao, ^^sono ^^un ^^bot. ^^Per ^^lamentele, ^^messaggia ^^[nofunallowed98765](https://www.reddit.com/user/nofunallowed98765/)[.](https://i.imgur.com/L67V6H4.gif)''' | |
@contextmanager | |
def login(username, password, user_agent): | |
session = praw.Reddit(user_agent = USER_AGENT) | |
try: | |
session.login(username = REDDIT_USERNAME, password = REDDIT_PASSWORD) | |
yield session | |
finally: | |
session.clear_authentication() | |
def archive(url): | |
r = requests.post('https://archive.today/submit/', {'url' : url, 'coo' : ""}) | |
return r.headers['refresh'][6:] | |
# Return True if we haven't already mirrored the ID | |
# else return False | |
def check_id(id): | |
with open('redditid.txt', 'r') as f: | |
return not (id in f) | |
# Double Check that we haven't already posted here | |
# By searching for the bot name in the comments | |
def double_check(comments): | |
for comment in comments: | |
if comment.author.name == REDDIT_USERNAME: | |
return False | |
return True | |
def write_id(id): | |
with open('redditid.txt', 'a') as f: | |
f.write(id + "\n") | |
def do_magic(): | |
with login(REDDIT_USERNAME, REDDIT_PASSWORD, USER_AGENT) as session: | |
sub = session.get_subreddit(SUBREDDIT) | |
posts = sub.get_new(limit = POST_LIMIT) | |
for post in posts: | |
if any(regex.match(post.url) for regex in SITES): | |
if check_id(post.id) and double_check(post.comments): | |
archive_url = archive(post.url) | |
post.add_comment(MESSAGE.format(url=archive_url, post_url=post.url)) | |
logging.info(post.id + " - " + post.url) | |
write_id(post.id) | |
if __name__ == "__main__": | |
logging.basicConfig(filename='rita.log' | |
,level=logging.INFO | |
,format='[%(asctime)s] - %(levelname)s - %(message)s') | |
while True: | |
try: | |
do_magic() | |
except Exception, e: | |
logging.exception(e) | |
time.sleep(REFRESH_TIME) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Rita bot | |
After=network.target | |
[Service] | |
User=rita | |
ExecStart=/usr/bin/python /home/rita/rita.py | |
WorkingDirectory=/home/rita/ | |
Restart=always | |
[Install] | |
WantedBy=multi-user.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment