swear01

Untitled

Oct 2nd, 2021
2,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. from re import S
  2. from pmaw import PushshiftAPI
  3. from datetime import datetime, tzinfo, timezone
  4. import json
  5. from tqdm import tqdm
  6. from pathlib import Path
  7.  
  8. sub_name = "shapezio"
  9. submission_limit = 10
  10. year = 2020
  11. workers = 60
  12.  
  13. '''
  14. reddit = praw.Reddit(
  15.    client_id = "J7tuNylJ6qvYQNXMhSrM2g",
  16.    client_secret = "f1OhB2biWcbKKyoF98AWeQGkbtnovA",
  17.    user_agent = "windows:grap_bot (by u/swear01)",
  18. )
  19. #'''
  20.  
  21. api = PushshiftAPI(num_workers = workers) #we don't need enrichment
  22. p = Path(".")
  23. if not (p / sub_name).is_dir() :(p/sub_name).mkdir()
  24. time_start = int(datetime(year,1,1,tzinfo=timezone.utc).timestamp())
  25. time_end = int(datetime(year+1,1,1,tzinfo=timezone.utc).timestamp())
  26. print(time_start,time_end)
  27. epoch = 0  
  28. while 1 :
  29.     stored = []
  30.     submission_ids = list(api.search_submissions(
  31.         subreddit = sub_name,        
  32.         after = time_start,
  33.         before = time_end,
  34.         limit = 10,
  35.         mem_safe = True
  36.     ))
  37.     print(1,epoch)
  38.     for submission_id in tqdm(submission_ids):  
  39.         print(4,epoch)      
  40.         s_data = []
  41.         s_data.append(str(submission_id))
  42.         comment_ids = api.search_submission_comment_ids(ids=submission_id)
  43.         print(5,epoch)  
  44.         for comment in tqdm(api.search_comments(ids=comment_ids)) :
  45.             print(3,epoch)
  46.             s_data.append([comment["id"],comment["body"],comment["score"]])
  47.         stored.append(s_data)
  48.     print(2,epoch)
  49.     with open(f"./{sub_name}/{epoch}.json","w") as f :
  50.         json.dump(stored,f,indent=2)
  51.  
  52.     if api.search_submissions(ids=submission_ids[-1]).time >= time_end : break
  53.     epoch+=1
  54.  
Advertisement
Add Comment
Please, Sign In to add comment