Advertisement
Guest User

update_database.py

a guest
Jan 30th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. import sqlite3
  2. import praw
  3.  
  4. reddit = praw.Reddit(
  5.     client_id='client_id',
  6.     client_secret='client_secret',
  7.     password='password',
  8.     user_agent='user_agent',
  9.     username='username'
  10. )
  11.  
  12. conn = sqlite3.connect('civworldpowers.db')
  13. c = conn.cursor()
  14.  
  15.  
  16. def outdated_ids():
  17.     """Get all id's from the database that have not yet been updated"""
  18.  
  19.     c.execute('''SELECT id FROM posts WHERE updated IS NOT "True"''')
  20.     for _id in c.fetchall():
  21.         yield _id[0]
  22.  
  23.  
  24. def flair_score(_id):
  25.     """Get the current flair and score for a particular submission"""
  26.  
  27.     sub = reddit.submission(_id)
  28.     return sub.link_flair_css_class, sub.score, 'True', _id
  29.  
  30.  
  31. def update_id(data):
  32.     """Update the flair and score for a submission"""
  33.  
  34.     while True:
  35.         try:
  36.             c.execute('''UPDATE posts SET flair = (?), score = (?),
  37.            updated = (?) WHERE id=(?)''', data)
  38.         except sqlite3.OperationalError:
  39.             continue
  40.         else:
  41.             conn.commit()
  42.             break
  43.  
  44.  
  45. def main():
  46.     """Get all submissions that have not yet been updated, get their current score and
  47.    flair and update the database with that information"""
  48.  
  49.     for _id in outdated_ids():
  50.         update_id(flair_score(_id))
  51.  
  52.  
  53. if __name__ == '__main__':
  54.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement