Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import websocket
  2. import config
  3. import praw
  4. import json
  5. import time
  6. import datetime
  7. import getpass
  8. import pytz
  9.  
  10. FIRST_MIDNIGHT = 1520917200 - 3600
  11. #FIRST_MIDNIGHT = 1519448403
  12.  
  13. DAY = 24 * 60 * 60
  14.  
  15. cur_update = 'LiveUpdate_0036af64-2673-11e8-b848-0e79ce9b2ccc'
  16.  
  17. def bot_login():
  18. print("Logging In...")
  19. login = praw.Reddit(username = config.username,
  20. password = config.password,
  21. client_id = config.client_id,
  22. client_secret = config.client_secret,
  23. user_agent = "Copies messages from livecounting to a separate thread for later reading")
  24. return login
  25.  
  26. def thread_about(thread):
  27. #Returns the live thread's about.json data, including the WebSocket (wss) URL, sidebar (resources) contents, etc.
  28.  
  29. # Get websocket_url from about.json
  30. response = login.request(
  31. method = 'GET',
  32. path = 'live/' + thread + '/about',
  33. params = { 'raw_json': 1 }
  34. );
  35.  
  36. # Return websocket_url from response
  37. return response;
  38.  
  39.  
  40. def get_old_updates(cur_update):
  41. return login.request(
  42. method = 'GET',
  43. path = 'live/' + config.thread,
  44. params = {
  45. 'before' : cur_update,
  46. 'limit' : '100'
  47. }
  48. );
  49.  
  50. def find_next_midnight(target, cur_update):
  51. updates = 0 # Because python is gross and I don't know a better way to give this scope here
  52. while True:
  53. updates = get_old_updates(cur_update)['data']['children']
  54.  
  55. #print(str(updates[0]['data']['created_utc']))
  56.  
  57. if updates[0]['data']['created_utc'] > target:
  58. break
  59.  
  60. cur_update = updates[0]['data']['name']
  61.  
  62. for i in range(len(updates)):
  63. if updates[i]['data']['created_utc'] < target:
  64. return (updates[i], updates[i-1], cur_update)
  65.  
  66. config.password = getpass.getpass("Password:")
  67.  
  68. login = bot_login()
  69.  
  70. current_midnight = FIRST_MIDNIGHT
  71.  
  72. for i in range(22):
  73. target_midnight = current_midnight + DAY
  74.  
  75. #print("Target: " + str(target_midnight))
  76.  
  77. (last, first, cur_update) = find_next_midnight(target_midnight, cur_update)
  78.  
  79. lasttime = datetime.datetime.fromtimestamp(last['data']['created_utc'])
  80. firsttime = datetime.datetime.fromtimestamp(first['data']['created_utc'])
  81.  
  82. print("Last update of " + lasttime.strftime('%Y-%m-%d') + " at " + lasttime.strftime('%H:%M:%S') + ": " + last['data']['name'])
  83.  
  84. print("First update of " + firsttime.strftime('%Y-%m-%d') + " at " + firsttime.strftime('%H:%M:%S') + ": " + first['data']['name'])
  85.  
  86. current_midnight = target_midnight
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement