-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_comments.py
74 lines (59 loc) · 2.14 KB
/
read_comments.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from dotenv import load_dotenv
import time
from datetime import datetime
import json
from praw import Reddit
from praw.models import MoreComments
# Reddit user object
load_dotenv()
reddit = Reddit(
client_id=str(os.getenv("CLIENT_ID")),
client_secret=str(os.getenv("CLIENT_SECRET")),
user_agent="reddit_reader by user")
URL = str(os.getenv("TARGET_URL"))
# function to extract time from a 2d array
def sorter(my_dict):
return my_dict['time']
# Reading the target post and its comments
post = reddit.submission(url = URL)
top_level_comments_obj = post.comments
top_level_comments = []
for comment in top_level_comments_obj:
corresponding_replies = []
for reply in comment.replies: # Fixing commentforest not dumpable in json error
corresponding_replies.append({'r_time': int(reply.created_utc)-25200, 'r_body': reply.body}) # -25200 to convert time to MST
top_level_comments.append({'time':int(comment.created_utc)-25200, 'body':comment.body, 'replies':corresponding_replies})
# Sorting top level comments in order of newest first
top_level_comments.sort(reverse=True, key=sorter)
# Retrieving previously used comments to compare and find unseen latest comments
try:
used_comments_obj = open('used_comments_file.json')
used_comments_json = json.load(used_comments_obj)
used_comments = used_comments_json['used_comments']
used_comments_obj.close()
except:
# print('Empty file')
used_comments = None
# Inserting the latest comments in the to_be_used list, in order of oldest first
to_be_used = []
if used_comments is not None:
for new_comment in top_level_comments:
if new_comment['time'] > used_comments[0]['time']:
to_be_used.insert(0, new_comment)
else:
break
elif used_comments is None:
for new_comment in top_level_comments:
to_be_used.insert(0, new_comment)
# Sorted in the order of latest/newest first, joined at start to previously used comments
if len(to_be_used) > 0:
now_used = to_be_used.copy()
now_used.sort(reverse=True, key=sorter)
try:
now_used.extend(used_comments)
except:
pass
used = {'used_comments': now_used}
with open('used_comments_file.json', 'w') as used_comments_file:
json.dump(used, used_comments_file)