2020-01-04 23:28:07 +02:00
|
|
|
import sys
|
2020-02-02 23:08:15 +02:00
|
|
|
import traceback
|
2019-12-30 23:16:39 +02:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
from random import randrange
|
2019-12-25 23:58:36 +02:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-01-20 23:54:10 +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.instagram = Instagram()
|
|
|
|
self.service_name = 'Instagram'
|
2020-11-15 21:55:59 +02:00
|
|
|
self.enabled = False
|
2019-12-25 23:58:36 +02:00
|
|
|
|
2020-01-10 21:59:59 +02:00
|
|
|
async def poll_implementation(self, bot, account, roomid, send_messages):
|
2020-01-05 00:03:37 +02:00
|
|
|
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.')
|
2020-01-05 00:03:37 +02:00
|
|
|
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}')
|
2020-01-05 00:03:37 +02:00
|
|
|
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")
|
2020-01-05 00:03:37 +02:00
|
|
|
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:')
|
2020-01-05 00:03:37 +02:00
|
|
|
traceback.print_exc(file=sys.stderr)
|
2019-12-25 23:58:36 +02:00
|
|
|
|
2019-12-30 23:16:39 +02:00
|
|
|
polldelay = timedelta(minutes=30 + randrange(30))
|
|
|
|
self.next_poll_time[roomid] = datetime.now() + polldelay
|