Advertisement
Guest User

logger.py

a guest
Jan 30th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.32 KB | None | 0 0
  1. from datetime import datetime
  2. import sqlite3
  3. import praw
  4.  
  5. reddit = praw.Reddit(
  6.     client_id='client_id',
  7.     client_secret='client_secret',
  8.     password='password',
  9.     user_agent='user_agent',
  10.     username='username'
  11. )
  12.  
  13. conn = sqlite3.connect('civworldpowers.db')
  14. c = conn.cursor()
  15.  
  16.  
  17. def build_db():
  18.     """Creates a new table the first time the script is ran on a new machine"""
  19.     c.execute('''CREATE TABLE IF NOT EXISTS posts(
  20.    date TEXT, time TEXT, user TEXT, flair TEXT, title TEXT, id TEXT, score TEXT, updated TEXT
  21.    )''')
  22.     conn.commit()
  23.  
  24.  
  25. def store(_data):
  26.     """Save the data to the database"""
  27.  
  28.     while True:
  29.         try:
  30.             c.execute('''INSERT INTO posts(date, time, user,
  31.            flair, title, id, score, updated) VALUES (?,?,?,?,?,?,?,?)''', _data)
  32.         except sqlite3.OperationalError:
  33.             continue
  34.         else:
  35.             conn.commit()
  36.             break
  37.  
  38.  
  39. def get_viewed():
  40.     """Extracts all historic post id's for deduplication"""
  41.     c.execute('''SELECT id FROM posts''')
  42.     return [post_id[0] for post_id in c.fetchall()]
  43.  
  44.  
  45. def translate_timestamp(timestamp):
  46.     """Translate a Unix timestamp into a tuple of date and time"""
  47.     string = datetime.fromtimestamp(int(timestamp)).strftime("%Y%m%d %H:%M:%S")
  48.     return string.split()
  49.  
  50.  
  51. def get_data(sub):
  52.     """Gather desired data into a tuple for storage"""
  53.     flair = sub.link_flair_css_class
  54.     date, _time = translate_timestamp(sub.created_utc)
  55.     try:
  56.         name = sub.author.name
  57.     except:
  58.         name = 'deleted'
  59.     return date, _time, name, flair, sub.title, sub.id, sub.score, 'False'
  60.  
  61.  
  62. def main():
  63.     """Monitor the subreddit and store submissions as they come
  64.    All posts will initially have updated set to False.
  65.    You must update the flair and score before calculating scores.
  66.    Flairs and scores can only be updated once so you should do it
  67.    immediately before calculating the results.
  68.    """
  69.  
  70.     viewed = get_viewed()
  71.     subreddit = reddit.subreddit('CivWorldPowers')
  72.     submission_stream = subreddit.stream.submissions()
  73.     for sub in submission_stream:
  74.         if sub.id in viewed:
  75.             continue
  76.         data = get_data(sub)
  77.         store(data)
  78.         viewed.append(sub.id)
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     build_db()
  83.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement