2020-01-10 21:59:59 +02:00
|
|
|
from twitterscraper import query_tweets_from_user
|
|
|
|
from modules.common.pollingservice import PollingService
|
|
|
|
|
|
|
|
# https://github.com/taspinar/twitterscraper/tree/master/twitterscraper
|
|
|
|
|
|
|
|
class MatrixModule(PollingService):
|
2020-01-20 23:54:10 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
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-01-20 23:54:10 +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)
|