Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. import re
  2. import time
  3. import praw
  4. import datetime
  5.  
  6. CONFIG_INTERVAL = 6 * 3600 # Number of seconds between updating
  7. CONFIG_CLIENT_ID = "" # Client ID from /prefs/apps
  8. CONFIG_CLIENT_SECRET = "" # Client secret from /prefs/apps
  9. CONFIG_USERNAME = "" # Username for the bot account
  10. CONFIG_PASSWORD = "" # Password for the bot account
  11. CONFIG_SUBREDDIT = "" # Subreddit(s) the script runs on
  12. CONFIG_SCRIPTHOST = "" # Your Reddit username
  13. CONFIG_USER_AGENT = "catsubtracker 1.0 for /r/%s, hosted by /u/%s" \
  14. %(CONFIG_SUBREDDIT, CONFIG_SCRIPTHOST)
  15. CONFIG_WIKIPAGE = "" # The name of the wiki page to check/edit
  16.  
  17. catmatch = r'indexing\s[0-9]*\scategories'
  18. submatch = r'categories\sand\s[0-9]*\ssubreddits'
  19. datemtch = r'Last updated [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC'
  20. pformat = 'The directory is currently indexing %d' + \
  21. ' categories and %d subreddits. (Last updated %s UTC)'
  22.  
  23. def get_counts(body):
  24. _catmatch = r'###\s*[^\s]*'
  25. _submatch = r'/r/[^\s]*'
  26. return (len(re.findall(_catmatch, body)),
  27. len(re.findall(_submatch, body)))
  28.  
  29. def get_old_counts(body):
  30. if 'The directory is currently' in body:
  31. return (int(re.findall(catmatch, body)[0].split(' ')[1]),
  32. int(re.findall(submatch, body)[0].split(' ')[2]))
  33. else:
  34. return (0, 0)
  35.  
  36. def gen_new_page(body, cats, subs):
  37. datestr = datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M')
  38. base = re.sub(catmatch, 'indexing %d categories'%cats, body)
  39. base = re.sub(submatch, 'categories and %d subreddits'%subs, base)
  40. base = re.sub(datemtch, 'Last updated %s UTC'%datestr, base)
  41. if base == body:
  42. base += '\r\n\r\n-------\r\n\r\n%s'%(pformat%(cats, subs, datestr))
  43. return base
  44.  
  45. def log(msg):
  46. print('[%s] %s'%(time.ctime(), msg))
  47.  
  48. def main():
  49. reddit = praw.Reddit(client_id = CONFIG_CLIENT_ID,
  50. client_secret = CONFIG_CLIENT_SECRET,
  51. username = CONFIG_USERNAME,
  52. password = CONFIG_PASSWORD,
  53. user_agent = CONFIG_USER_AGENT)
  54. while True:
  55. log('Loading wiki page...')
  56. wikipage = reddit.subreddit(CONFIG_SUBREDDIT).wiki[CONFIG_WIKIPAGE]
  57. cats, subs = get_counts(wikipage.content_md)
  58. ocats, osubs = get_old_counts(wikipage.content_md)
  59. if cats != ocats or subs != osubs:
  60. log('Directory has been updated! Editing...')
  61. new_page = gen_new_page(wikipage.content_md, cats, subs)
  62. wikipage.edit(new_page, 'Category / sub count updated.')
  63. log('Directory edited.')
  64. else:
  65. log('No changes were detected.')
  66. log('Sleeping for %d hours.'%(CONFIG_INTERVAL / 3600))
  67. time.sleep(CONFIG_INTERVAL)
  68.  
  69. if __name__ == '__main__':
  70. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement