lunarflu HF Staff commited on
Commit
25114ff
·
verified ·
1 Parent(s): 10a59bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -27
app.py CHANGED
@@ -1,8 +1,7 @@
1
  import os
2
- import gradio as gr
3
  import praw
4
- import asyncio
5
 
 
6
  print("Initializing Reddit instance...")
7
 
8
  client_id = os.getenv('CLIENT_ID')
@@ -11,12 +10,14 @@ user_agent = os.getenv('USER_AGENT')
11
  username = os.getenv('REDDIT_USERNAME')
12
  password = os.getenv('REDDIT_PASSWORD')
13
 
 
14
  print(f"Client ID: {client_id}")
15
  print(f"Client Secret is set: {'Yes' if client_secret else 'No'}")
16
  print(f"User Agent: {user_agent}")
17
  print(f"Username: {username}")
18
  print(f"Password is set: {'Yes' if password else 'No'}")
19
 
 
20
  if not all([client_id, client_secret, user_agent, username, password]):
21
  print("Error: One or more environment variables are missing.")
22
  exit(1)
@@ -32,12 +33,12 @@ try:
32
  print("Reddit instance created successfully.")
33
  except Exception as e:
34
  print(f"Error creating Reddit instance: {e}")
35
- exit(1)
36
 
37
- async def monitor_new_posts():
38
  try:
39
  print("Attempting to access subreddit...")
40
- subreddit_name = 'politics'
41
  subreddit = reddit.subreddit(subreddit_name)
42
  print(f"Successfully accessed subreddit: {subreddit.display_name}")
43
  except Exception as e:
@@ -46,31 +47,18 @@ async def monitor_new_posts():
46
 
47
  print("Starting to monitor new posts...")
48
  try:
49
- while True:
50
- for submission in await asyncio.to_thread(lambda: list(subreddit.stream.submissions(skip_existing=True))):
51
- print(f"New post detected in r/{subreddit.display_name}:")
52
- print(f"Title: {submission.title}")
53
- print(f"Author: {submission.author}")
54
- print(f"URL: {submission.url}")
55
- print(f"Content: {submission.selftext}\n")
56
  except Exception as e:
57
  print(f"Error during streaming submissions: {e}")
58
 
59
- async def main():
60
- print("Starting main function...")
61
- await monitor_new_posts()
62
-
63
- def greet(name):
64
- return "Hello " + name + "!"
65
-
66
  if __name__ == "__main__":
67
  try:
68
- #demo = gr.Interface(fn=greet, inputs="text", outputs="text")
69
- #demo.launch()
70
- asyncio.run(main())
71
-
72
  except Exception as e:
73
- print(f"An error occurred in the main function: {e}")
74
-
75
-
76
-
 
1
  import os
 
2
  import praw
 
3
 
4
+ # Reddit API credentials retrieved from environment variables
5
  print("Initializing Reddit instance...")
6
 
7
  client_id = os.getenv('CLIENT_ID')
 
10
  username = os.getenv('REDDIT_USERNAME')
11
  password = os.getenv('REDDIT_PASSWORD')
12
 
13
+ # Print out the values to ensure they are loaded (mask sensitive info)
14
  print(f"Client ID: {client_id}")
15
  print(f"Client Secret is set: {'Yes' if client_secret else 'No'}")
16
  print(f"User Agent: {user_agent}")
17
  print(f"Username: {username}")
18
  print(f"Password is set: {'Yes' if password else 'No'}")
19
 
20
+ # Check if all credentials are retrieved successfully
21
  if not all([client_id, client_secret, user_agent, username, password]):
22
  print("Error: One or more environment variables are missing.")
23
  exit(1)
 
33
  print("Reddit instance created successfully.")
34
  except Exception as e:
35
  print(f"Error creating Reddit instance: {e}")
36
+ exit(1) # Exit if Reddit instance cannot be created
37
 
38
+ def monitor_new_posts():
39
  try:
40
  print("Attempting to access subreddit...")
41
+ subreddit_name = 'politics' # Replace with your subreddit
42
  subreddit = reddit.subreddit(subreddit_name)
43
  print(f"Successfully accessed subreddit: {subreddit.display_name}")
44
  except Exception as e:
 
47
 
48
  print("Starting to monitor new posts...")
49
  try:
50
+ for submission in subreddit.stream.submissions(skip_existing=True):
51
+ print(f"New post detected in r/{subreddit.display_name}:")
52
+ print(f"Title: {submission.title}")
53
+ print(f"Author: {submission.author}")
54
+ print(f"URL: {submission.url}")
55
+ print(f"Content: {submission.selftext}\n")
 
56
  except Exception as e:
57
  print(f"Error during streaming submissions: {e}")
58
 
 
 
 
 
 
 
 
59
  if __name__ == "__main__":
60
  try:
61
+ print("Starting main function...")
62
+ monitor_new_posts()
 
 
63
  except Exception as e:
64
+ print(f"An error occurred in the main function: {e}")