Guest User

Untitled

a guest
Dec 15th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. import os
  2. import shutil
  3. import praw
  4. from datetime import datetime
  5. from time import strftime
  6. from userpw import *
  7.  
  8. try:
  9. reddit = praw.Reddit(client_id=client_id, client_secret=client_secret,
  10. username=username, password=password,
  11. user_agent=user_agent)
  12.  
  13. sub = reddit.subreddit('cryptocurrency')
  14. posts = sub.hot(limit=27)
  15. except:
  16. with open('errorlog.txt', 'a') as e:
  17. e.write("{} - error getting reddit data\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  18.  
  19. tablelist = [["Time sampled", "Post id", "Title", "Score", "10m ago", "20m ago", "30m ago", "40m ago", "50m ago", "1h ago"]]
  20.  
  21. try:
  22. os.rename('rcryptocurrency.html', './archives/{}rcryptocurrency.html'.format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  23. except:
  24. # print("Failed to move html file")
  25. with open('errorlog.txt', 'a') as e:
  26. e.write("{} - error moving rcryptocurrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  27. try:
  28. os.rename('posts60min.txt', 'posts70min.txt')
  29. os.rename('posts50min.txt', 'posts60min.txt')
  30. os.rename('posts40min.txt', 'posts50min.txt')
  31. os.rename('posts30min.txt', 'posts40min.txt')
  32. os.rename('posts20min.txt', 'posts30min.txt')
  33. os.rename('posts10min.txt', 'posts20min.txt')
  34. os.rename('posts.txt', 'posts10min.txt')
  35. except:
  36. with open('errorlog.txt', 'a') as e:
  37. e.write("{} - error moving posts files\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  38.  
  39. try:
  40. with open('posts.txt', 'w') as f:
  41. for i in posts:
  42. # print(i.title)
  43. f.write('{},,,{},,,{},,,{}'.format(datetime.now().strftime('%Y%m%d %H:%M'), i.id, i.title.encode("ascii", "replace"), i.score) + "\n")
  44. try:
  45. 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"])
  46. except:
  47. with open('errorlog.txt', 'a') as e:
  48. e.write("{} - error appending tablelist\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  49. except:
  50. with open('errorlog.txt', 'a') as e:
  51. e.write("{} - error writing to posts.txt\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  52.  
  53. try:
  54. with open('posts10min.txt', 'r') as f:
  55. for l in f:
  56. if len(l) > 10:
  57. oldscore = l.split(',,,')[3]
  58. oldid = l.split(',,,')[1]
  59. for val in tablelist:
  60. if val[1] == oldid:
  61. val[4] = oldscore
  62.  
  63. with open('posts20min.txt', 'r') as f:
  64. for l in f:
  65. if len(l) > 10:
  66. oldscore = l.split(',,,')[3]
  67. oldid = l.split(',,,')[1]
  68. for val in tablelist:
  69. if val[1] == oldid:
  70. val[5] = oldscore
  71.  
  72. with open('posts30min.txt', 'r') as f:
  73. for l in f:
  74. if len(l) > 10:
  75. oldscore = l.split(',,,')[3]
  76. oldid = l.split(',,,')[1]
  77. for val in tablelist:
  78. if val[1] == oldid:
  79. val[6] = oldscore
  80.  
  81. with open('posts40min.txt', 'r') as f:
  82. for l in f:
  83. if len(l) > 10:
  84. oldscore = l.split(',,,')[3]
  85. oldid = l.split(',,,')[1]
  86. for val in tablelist:
  87. if val[1] == oldid:
  88. val[7] = oldscore
  89.  
  90. with open('posts50min.txt', 'r') as f:
  91. for l in f:
  92. if len(l) > 10:
  93. oldscore = l.split(',,,')[3]
  94. oldid = l.split(',,,')[1]
  95. for val in tablelist:
  96. if val[1] == oldid:
  97. val[8] = oldscore
  98.  
  99. with open('posts60min.txt', 'r') as f:
  100. for l in f:
  101. if len(l) > 10:
  102. oldscore = l.split(',,,')[3]
  103. oldid = l.split(',,,')[1]
  104. for val in tablelist:
  105. if val[1] == oldid:
  106. val[9] = oldscore
  107. except:
  108. with open('errorlog.txt', 'a') as e:
  109. e.write("{} - error reading old posts.txt\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  110.  
  111. # print(tablelist)
  112.  
  113. try:
  114. with open('rcryptocurrency.html', 'w') as f:
  115. f.write("<table styel='width:100%'>\n")
  116. for i in tablelist:
  117. f.write(" <tr>\n")
  118. for j in i:
  119. try:
  120. f.write(" <td> {} </td>".format(j.encode("ascii", "replace")))
  121. except:
  122. f.write(" <td> {} </td>".format(j))
  123. f.write("\n </tr>\n")
  124. f.write("</table>")
  125. except:
  126. with open('errorlog.txt', 'a') as e:
  127. e.write("{} - error writing new rcryptocrrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
  128.  
  129. try:
  130. shutil.copy('rcryptocurrency.html', '/root/www/')
  131. except:
  132. with open('errorlog.txt', 'a') as e:
  133. e.write("{} - error copying new cryptocurrency.html\n".format(datetime.now().strftime('%Y-%m-%d_%H%M')))
Add Comment
Please, Sign In to add comment