-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
81 lines (61 loc) · 2.54 KB
/
bot.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
75
76
77
78
79
80
81
import logging.config
import time
from config.bot_config import BotConfig
from news import news_grabber, news_api_auth
from twitter import followers, tweets, twitter_api_auth
# Initialize and configure logger
logging.config.fileConfig('logging.config')
logger = logging.getLogger('bot')
# Initialize news api
news_api = news_api_auth.create_api()
news_grabber = news_grabber.NewsGrabber(news_api)
# Initialize twitter api
tw_api = twitter_api_auth.create_api()
tw_handler = tweets.TweetsHandler(tw_api)
followers_handler = followers.FollowersHandler(tw_api)
# Initialize constants
bot_config = BotConfig()
bot_config.initialize()
SLEEP_TIME = int(bot_config.get_property_value('BOT_SLEEP_TIME'))
COUNTRY = bot_config.get_property_value('BOT_COUNTRY')
CATEGORY = bot_config.get_property_value('BOT_CATEGORY')
LANGUAGE = bot_config.get_property_value('BOT_LANGUAGE')
HASHTAG = bot_config.get_property_value('BOT_HASHTAG')
KEYWORDS = bot_config.get_property_values('BOT_QUERIES')
BL = bot_config.get_property_values('BOT_BLACKLISTED_SOURCES')
def get_news(query=None, category=None, country=None, language='en'):
return news_grabber.get_news(query=query, category=category, country=country, language=language, blacklist=BL)
def tweet_news(news_item, hash_tag=None):
return tw_handler.post_new_tweet(text=news_item.description, source=news_item.url, hashtag=hash_tag)
def collect_news():
top_news = []
if len(KEYWORDS) == 0:
return get_news(country=COUNTRY, category=CATEGORY, language=LANGUAGE)
for keyword in KEYWORDS:
k_news = get_news(query=keyword, language=LANGUAGE)
if len(k_news) > 0:
logger.info(f'Found %d news for keyword %s', len(k_news), keyword)
top_news = top_news + k_news
else:
logger.info(f'I did not find any news for keyword %s', keyword)
return top_news
def sleep():
logging.info(f'Taking a %s minutes break...', SLEEP_TIME / 60)
time.sleep(SLEEP_TIME)
logger.info('OK I am ready to continue. Lets tweet something again...')
def main():
while True:
# follow back new followers
# followers_handler.follow_followers()
news = collect_news()
if len(news) == 0:
logger.info('There are no news at the moment')
for n in news:
hash_tag = '#' + HASHTAG + ' #' + n.query
logger.info('Tweet attempt about ' + n.query)
result = tweet_news(news_item=n, hash_tag=hash_tag)
if result is not None:
sleep()
sleep()
if __name__ == '__main__':
main()