Skip to content

Instantly share code, notes, and snippets.

@shatteringlass
Forked from Spittie/rita.py
Last active August 29, 2015 14:18
Show Gist options
  • Save shatteringlass/3af7fb4167630e97a039 to your computer and use it in GitHub Desktop.
Save shatteringlass/3af7fb4167630e97a039 to your computer and use it in GitHub Desktop.
# Bot that mirror some websites from /r/italy to archive.today
# Yeah, this code sucks. Deal with it.
# Consider yourself lucky to not have seen the first version.
from __future__ import print_function
import praw
import re
import requests
import time
REDDIT_USERNAME = 'calcolatore'
REDDIT_PASSWORD = 'nicetry'
USER_AGENT = ("RitaBot 0.2 for /r/italy by /u/nofunallowed98765")
SITES = [
re.compile('.+corriere\.it.*'),
re.compile('.+beppegrillo\.it.*'),
re.compile('.+ilmanifesto\.info.*')
]
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={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)'''
def login():
p = praw.Reddit(user_agent = USER_AGENT)
p.login(username = REDDIT_USERNAME, password = REDDIT_PASSWORD)
return p
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:
return not (comment.author.name == REDDIT_USERNAME)
def write_id(id):
with open('redditid.txt', 'a') as f:
f.write(id)
def do_magic():
p = login()
sub = p.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))
write_id(post.id)
while True:
do_magic()
time.sleep(REFRESH_TIME)
[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