-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdistributors.py
More file actions
155 lines (135 loc) · 5.18 KB
/
distributors.py
File metadata and controls
155 lines (135 loc) · 5.18 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
import tweepy
from telegram.ext import Updater
import logging
from app import db
from models import Article, DistributionLog
import os
import time
from requests.exceptions import RequestException
class TwitterDistributor:
def __init__(self):
self.api = None
self.credentials_valid = False
def setup_twitter(self):
"""Setup Twitter API with proper error handling"""
try:
api_key = os.environ.get("TWITTER_API_KEY")
api_secret = os.environ.get("TWITTER_API_SECRET")
access_token = os.environ.get("TWITTER_ACCESS_TOKEN")
access_token_secret = os.environ.get("TWITTER_ACCESS_TOKEN_SECRET")
if not all([api_key, api_secret, access_token, access_token_secret]):
logging.error("Twitter credentials not configured. Please set all required environment variables.")
return False
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
self.api = tweepy.API(auth)
self.credentials_valid = True
return True
except Exception as e:
logging.error(f"Twitter setup error: {str(e)}")
return False
def post_article(self, article):
try:
if not self.credentials_valid and not self.setup_twitter():
log = DistributionLog(
article_id=article.id,
platform='twitter',
status='error',
message='Invalid Twitter credentials'
)
db.session.add(log)
db.session.commit()
return False
tweet_text = f"{article.title}\n\nRead more: {article.source_url}"
self.api.update_status(tweet_text)
log = DistributionLog(
article_id=article.id,
platform='twitter',
status='success'
)
db.session.add(log)
db.session.commit()
return True
except Exception as e:
logging.error(f"Twitter posting error: {str(e)}")
log = DistributionLog(
article_id=article.id,
platform='twitter',
status='error',
message=str(e)
)
db.session.add(log)
db.session.commit()
return False
class TelegramDistributor:
def __init__(self):
self.bot = None
self.credentials_valid = False
def setup_telegram(self):
"""Setup Telegram bot with proper error handling"""
try:
bot_token = os.environ.get("TELEGRAM_BOT_TOKEN")
if not bot_token:
logging.error("Telegram bot token not configured")
return False
self.updater = Updater(token=bot_token, use_context=True)
self.bot = self.updater.bot
self.credentials_valid = True
return True
except Exception as e:
logging.error(f"Telegram setup error: {str(e)}")
return False
def send_article(self, article):
try:
if not self.credentials_valid and not self.setup_telegram():
log = DistributionLog(
article_id=article.id,
platform='telegram',
status='error',
message='Invalid Telegram credentials'
)
db.session.add(log)
db.session.commit()
return False
channel_id = os.environ.get("TELEGRAM_CHANNEL_ID")
if not channel_id:
logging.error("Telegram channel ID not configured")
return False
message = f"*{article.title}*\n\n{article.summary}\n\n[Read more]({article.source_url})"
self.bot.send_message(
chat_id=channel_id,
text=message,
parse_mode='Markdown'
)
log = DistributionLog(
article_id=article.id,
platform='telegram',
status='success'
)
db.session.add(log)
db.session.commit()
return True
except Exception as e:
logging.error(f"Telegram posting error: {str(e)}")
log = DistributionLog(
article_id=article.id,
platform='telegram',
status='error',
message=str(e)
)
db.session.add(log)
db.session.commit()
return False
def distribute_articles():
"""Distribute processed articles that haven't been published yet"""
articles = Article.query.filter_by(published=False).all()
logging.info(f"Found {len(articles)} unpublished articles to distribute")
for article in articles:
try:
# Mark as published without social media distribution
article.published = True
db.session.commit()
logging.info(f"Marked article {article.id} as published")
except Exception as e:
logging.error(f"Distribution error for article {article.id}: {str(e)}")
continue