RazgrizX

r/GoForGold - Check last time seen on Reddit

Feb 9th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import requests
  2. import json
  3. from datetime import datetime
  4.  
  5. actions = ["comments", "submitted"]
  6. url = "https://www.reddit.com/{}/{}/{}.json?limit=1"
  7.  
  8. def main():
  9.     show_menu()
  10.  
  11. def show_menu():
  12.     print("Welcome to the stalker app for Reddit!\n")
  13.     target = input("Who do you wanna stalk?\nu/").lower()
  14.    
  15.     if check_activity(target):
  16.         print("Active in the last hour.")
  17.     else:
  18.         print("Inactive in the last hour.")
  19.  
  20. def check_activity(target):
  21.     was_active = False
  22.  
  23.     for action in actions:
  24.         if was_active:
  25.             break
  26.  
  27.         info = get_target_info("user", target, action)
  28.         last_date = get_date_from_info(info)
  29.  
  30.         if last_date is not None:
  31.             time_elapsed = (datetime.utcnow() - last_date).seconds / 3600
  32.             was_active = time_elapsed <= 1
  33.    
  34.     return was_active
  35.  
  36.  
  37. def get_date_from_info(info):
  38.     entries = info["data"]["children"]
  39.    
  40.     if len(entries) > 0:
  41.         return datetime.fromtimestamp(entries[0]["data"]["created_utc"])
  42.     else:
  43.         return None
  44.  
  45.  
  46. def get_target_info(subject, target, action):
  47.     result = requests.get(url.format(subject, target, action), headers = {"User-Agent": "Last seen on Reddit"})
  48.  
  49.     if result.status_code == 404:
  50.         print("User does not exist")
  51.         input("Press enter to exit")
  52.         exit(-1)
  53.  
  54.     return result.json()
  55.  
  56.  
  57. if __name__ == "__main__":
  58.     main()
  59.  
  60. input("Press enter to exit.")
Add Comment
Please, Sign In to add comment