Skip to content

Instantly share code, notes, and snippets.

@jwinterm
Created December 13, 2017 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwinterm/a89573f9f1b36bd1205af051da2528ee to your computer and use it in GitHub Desktop.
Save jwinterm/a89573f9f1b36bd1205af051da2528ee to your computer and use it in GitHub Desktop.
import os
import shutil
import praw
from datetime import datetime
from time import strftime
from userpw import *
try:
reddit = praw.Reddit(client_id=client_id, client_secret=client_secret,
username=username, password=password,
user_agent=user_agent)
sub = reddit.subreddit('cryptocurrency')
posts = sub.hot(limit=27)
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error getting reddit data\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
tablelist = [["Time sampled", "Post id", "Title", "Score", "10m ago", "20m ago", "30m ago", "40m ago", "50m ago", "1h ago"]]
try:
os.rename('rcryptocurrency.html', './archives/{}rcryptocurrency.html'.format(datetime.now().strftime('%Y-%m-%d_%H%M')))
except:
# print("Failed to move html file")
with open('errorlog.txt', 'a') as e:
e.write("{} - error moving rcryptocurrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
try:
os.rename('posts60min.txt', 'posts70min.txt')
os.rename('posts50min.txt', 'posts60min.txt')
os.rename('posts40min.txt', 'posts50min.txt')
os.rename('posts30min.txt', 'posts40min.txt')
os.rename('posts20min.txt', 'posts30min.txt')
os.rename('posts10min.txt', 'posts20min.txt')
os.rename('posts.txt', 'posts10min.txt')
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error moving posts files\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
try:
with open('posts.txt', 'w') as f:
for i in posts:
# print(i.title)
f.write('{},,,{},,,{},,,{}'.format(datetime.now().strftime('%Y%m%d %H:%M'), i.id, i.title.encode("ascii", "replace"), i.score) + "\n")
try:
tablelist.append([datetime.now().strftime('%Y-%m-%d %H:%M'), i.id, i.title, i.score, "n/a", "n/a", "n/a", "n/a", "n/a", "n/a"])
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error appending tablelist\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error writing to posts.txt\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
try:
with open('posts10min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[4] = oldscore
with open('posts20min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[5] = oldscore
with open('posts30min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[6] = oldscore
with open('posts40min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[7] = oldscore
with open('posts50min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[8] = oldscore
with open('posts60min.txt', 'r') as f:
for l in f:
if len(l) > 10:
oldscore = l.split(',,,')[3]
oldid = l.split(',,,')[1]
for val in tablelist:
if val[1] == oldid:
val[9] = oldscore
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error reading old posts.txt\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
# print(tablelist)
try:
with open('rcryptocurrency.html', 'w') as f:
f.write("<table styel='width:100%'>\n")
for i in tablelist:
f.write(" <tr>\n")
for j in i:
try:
f.write(" <td> {} </td>".format(j.encode("ascii", "replace")))
except:
f.write(" <td> {} </td>".format(j))
f.write("\n </tr>\n")
f.write("</table>")
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error writing new rcryptocrrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
try:
shutil.copy('rcryptocurrency.html', '/root/www/')
except:
with open('errorlog.txt', 'a') as e:
e.write("{} - error copying new cryptocurrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment