2020-02-06 01:19:45 +02:00
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
|
2020-01-10 21:59:59 +02:00
|
|
|
from twitterscraper import query_tweets_from_user
|
2020-02-02 23:08:15 +02:00
|
|
|
|
2020-01-10 21:59:59 +02:00
|
|
|
from modules.common.pollingservice import PollingService
|
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
|
2020-01-10 21:59:59 +02:00
|
|
|
# https://github.com/taspinar/twitterscraper/tree/master/twitterscraper
|
|
|
|
|
2020-02-02 23:08:15 +02:00
|
|
|
class MatrixModule(PollingService):
|
2020-02-06 21:56:53 +02:00
|
|
|
def __init__(self, name):
|
|
|
|
super().__init__(name)
|
2020-01-20 23:54:10 +02:00
|
|
|
self.service_name = 'Twitter'
|
2020-01-10 21:59:59 +02:00
|
|
|
|
|
|
|
async def poll_implementation(self, bot, account, roomid, send_messages):
|
2020-01-10 22:19:49 +02:00
|
|
|
try:
|
2020-01-20 23:54:10 +02:00
|
|
|
tweets = query_tweets_from_user(account, limit=1)
|
|
|
|
print(f'Polling twitter account {account} - got {len(tweets)} tweets')
|
|
|
|
for tweet in tweets:
|
2020-01-10 22:19:49 +02:00
|
|
|
if tweet.tweet_id not in self.known_ids:
|
|
|
|
if send_messages:
|
2020-02-02 23:08:15 +02:00
|
|
|
await bot.send_html(bot.get_room_by_id(roomid),
|
|
|
|
f'<a href="https://twitter.com{tweet.tweet_url}">Twitter {account}</a>: {tweet.text}',
|
|
|
|
f'Twitter {account}: {tweet.text} - https://twitter.com{tweet.tweet_url}')
|
2020-01-10 22:19:49 +02:00
|
|
|
self.known_ids.add(tweet.tweet_id)
|
|
|
|
except Exception:
|
|
|
|
print('Polling twitter account failed:')
|
|
|
|
traceback.print_exc(file=sys.stderr)
|