|
5 | 5 | import re
|
6 | 6 | import collections
|
7 | 7 | import time
|
| 8 | +import json |
8 | 9 |
|
9 | 10 | # Open the text file containing the words
|
10 | 11 | with open('wholesomewords.txt', 'r') as f:
|
|
32 | 33 | cache = {}
|
33 | 34 |
|
34 | 35 | # Subreddit and trigger phrase
|
35 |
| -subreddit_names = "IndianTeenagers, flatapartmentcheck, JEENEETards" |
| 36 | +subreddit_names = "flatapartmentcheck+IndianTeenagers+JEENEETards" |
36 | 37 | trigger_phrases = ['!wholesomenesscheck',
|
37 | 38 | '!wholesomecheck', '!uwucheck', '!uwucheckself']
|
38 | 39 |
|
|
43 | 44 | comments_to_reply = []
|
44 | 45 | for subreddit_name in subreddit_names:
|
45 | 46 | subreddit = reddit.subreddit(
|
46 |
| - "IndianTeenagers+flatapartmentcheck+JEENEETards") |
| 47 | + "flatapartmentcheck+IndianTeenagers+JEENEETards") |
47 | 48 | for incoming_comment in subreddit.stream.comments(skip_existing=True):
|
48 | 49 | # Check if comment contains any of the trigger phrases
|
49 | 50 | if any(phrase in incoming_comment.body.lower() for phrase in trigger_phrases):
|
|
75 | 76 | else:
|
76 | 77 | # Make API request for user comments
|
77 | 78 | url = f'https://api.pushshift.io/reddit/comment/search?html_decode=true&after=0&author={user_name}&size=500'
|
78 |
| - with requests.Session() as session: |
79 |
| - response = session.get(url) |
80 |
| - # Add a sleep of 10 seconds after each API request |
81 |
| - time.sleep(10) |
82 |
| - api_comments = response.json()['data'] |
| 79 | + try: |
| 80 | + with requests.Session() as session: |
| 81 | + response = session.get(url) |
| 82 | + # Add a sleep of 7 seconds after each API request |
| 83 | + time.sleep(7) |
| 84 | + api_comments = response.json()['data'] |
| 85 | + except (requests.exceptions.RequestException, json.JSONDecodeError) as e: |
| 86 | + print( |
| 87 | + f"Error occurred while making the API request: {e}") |
| 88 | + api_comments = [] |
83 | 89 |
|
84 | 90 | # Store response in cache
|
85 | 91 | cache[user_name] = {
|
|
117 | 123 | reply_text = f'The number of wholesome occurrences in the recent 500 comments of u/{user_name} is {wholesome_count}.\n\n| Word | Count |\n| --- | --- |\n{table} |\n\nStay wholesome! \n\nWanna do something even more wholesome? Leave a \u2B50 at the [GitHub repository](https://github.com/MeowthyVoyager/reddit-wholesome-counter).'
|
118 | 124 |
|
119 | 125 | # Add comment to list of comments to reply to
|
| 126 | + comments_to_reply.append((incoming_comment, reply_text)) |
| 127 | + |
| 128 | + # Call reply() on the comment immediately |
120 | 129 | incoming_comment.reply(reply_text)
|
0 commit comments