-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
158 lines (129 loc) · 5.09 KB
/
bot.py
File metadata and controls
158 lines (129 loc) · 5.09 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import time
import json
from requests_oauthlib import OAuth1Session
import os
from utils import (
fetch_news,
summarize_article,
format_tweet,
fetch_config_from_appwrite,
)
NEWS_API_KEY = os.getenv("NEWS_API_KEY")
def post_tweet(tweet_text):
"""Post a tweet using Twitter API v2"""
# Set up OAuth1Session
oauth = OAuth1Session(
X_API_KEY,
client_secret=X_API_SECRET,
resource_owner_key=X_ACCESS_TOKEN,
resource_owner_secret=X_ACCESS_SECRET,
)
# Create payload with tweet text
payload = {"text": tweet_text}
# Make the API request
response = oauth.post(
"https://api.twitter.com/2/tweets",
json=payload,
)
# Check if the request was successful
if response.status_code != 201:
print(f"Error posting tweet: {response.status_code} {response.text}")
return False
# Print success message
print("Tweet posted successfully!")
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))
return True
def is_niche_related(article, summary, niche_keywords):
"""Check if the article is genuinely about the given niche"""
summary = summary or ""
# Combine title, description, and summary text for checking
text_to_check = (
article.get("title", "") + " " + article.get("description", "") + " " + summary
).lower()
# Convert text to a proper format for word boundary checking
text_to_check = " " + text_to_check + " "
# Check if any niche keyword is present (with word boundaries)
for keyword in niche_keywords:
search_term = f" {keyword} "
if search_term in text_to_check:
print(f"Niche keyword found: '{keyword}' in article")
return True
print("No niche keywords found in article")
return False
def run_bot():
# Fetch configuration from Appwrite
configs = fetch_config_from_appwrite()
if not configs:
print("No configurations found.")
return
for config in configs:
print(f"Running bot for config: {config.get('X_API_KEY')}")
# Populate variables
global X_API_KEY, X_API_SECRET, X_ACCESS_TOKEN, X_ACCESS_SECRET, tone, past_tweets
X_API_KEY = config["X_API_KEY"]
X_API_SECRET = config["X_API_SECRET"]
X_ACCESS_TOKEN = config["X_ACCESS_TOKEN"]
X_ACCESS_SECRET = config["X_ACCESS_SECRET"]
tone = config["Tone"]
past_tweets = config["Past_tweets"]
# Convert the comma-separated topics string into a list
topics = [topic.strip() for topic in config["Topics"].split(",")]
# Fetch the news for the first topic in the list
primary_topic = topics[0]
news = fetch_news(NEWS_API_KEY, primary_topic)
articles_posted = 0
candidate_tweets = []
# For each article, summarize and collect candidates with a keyword score
for article in news:
print("Processing article:", article.get("title", "No title"))
summary = summarize_article(
article["content"],
primary_topic,
tone,
past_tweets,
)
# Check if the article is related to any topic in the list
niche_keywords = [t.lower() for t in topics]
if not is_niche_related(article, summary, niche_keywords):
print(f"Skipping article: {article.get('title', 'No title')}")
continue
tweet = format_tweet(article, summary)
print("Tweet candidate:", tweet)
# Prepare text for keyword counting (using title, description, and summary)
text_to_check = (
article.get("title", "")
+ " "
+ article.get("description", "")
+ " "
+ summary
).lower()
text_to_check = " " + text_to_check + " "
# Count how many distinct keywords appear
keyword_count = sum(
1 for keyword in niche_keywords if f" {keyword} " in text_to_check
)
candidate_tweets.append((keyword_count, tweet))
# Select the candidate tweet with the highest keyword count
if candidate_tweets:
best_candidate = max(candidate_tweets, key=lambda x: x[0])
best_tweet = best_candidate[1]
print("Best tweet selected based on keywords:")
print("Tweet:", best_tweet)
retries = 3
while retries > 0:
success = post_tweet(best_tweet)
# success = True
if success:
articles_posted += 1
break
retries -= 1
print("Retrying to post the tweet...")
time.sleep(15) # Wait before retrying
print(
f"Bot run complete for config {config.get('X_API_KEY')}. Posted {articles_posted} articles for topics: {', '.join(topics)}.\n"
)
# Call the run_bot function directly when the script is executed
if __name__ == "__main__":
print("Running the bot...")
run_bot()