hemppa/modules/ig.py

41 lines
1.6 KiB
Python
Raw Normal View History

import sys
2020-02-02 23:08:15 +02:00
import traceback
from datetime import datetime, timedelta
from random import randrange
2020-01-02 14:27:29 +02:00
from igramscraper.exception.instagram_not_found_exception import \
InstagramNotFoundException
from igramscraper.instagram import Instagram
2020-02-02 23:08:15 +02:00
from modules.common.pollingservice import PollingService
class MatrixModule(PollingService):
def __init__(self, name):
super().__init__(name)
self.instagram = Instagram()
self.service_name = 'Instagram'
async def poll_implementation(self, bot, account, roomid, send_messages):
try:
medias = self.instagram.get_medias(account, 5)
2020-02-08 23:16:19 +02:00
self.logger.info(f'Polling instagram account {account} for room {roomid} - got {len(medias)} posts.')
for media in medias:
if send_messages:
if media.identifier not in self.known_ids:
2020-02-02 23:08:15 +02:00
await bot.send_html(bot.get_room_by_id(roomid),
f'<a href="{media.link}">Instagram {account}:</a> {media.caption}',
f'{account}: {media.caption} {media.link}')
self.known_ids.add(media.identifier)
except InstagramNotFoundException:
2020-02-08 23:16:19 +02:00
self.logger.error(f"{account} does not exist - deleting from room")
self.account_rooms[roomid].remove(account)
bot.save_settings()
except Exception:
2020-02-08 23:16:19 +02:00
self.logger.error('Polling instagram account failed:')
traceback.print_exc(file=sys.stderr)
polldelay = timedelta(minutes=30 + randrange(30))
self.next_poll_time[roomid] = datetime.now() + polldelay