hemppa/modules/ig.py

120 lines
4.8 KiB
Python
Raw Normal View History

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
class MatrixModule:
instagram = Instagram()
known_ids = set()
2020-01-02 14:27:29 +02:00
account_rooms = dict() # Roomid -> [account, account..]
next_poll_time = dict() # Roomid -> datetime, None = not polled yet
async def matrix_poll(self, bot, pollcount):
if len(self.account_rooms):
await self.poll_all_accounts(bot)
async def poll_all_accounts(self, bot):
now = datetime.now()
for roomid in self.account_rooms:
2019-12-30 23:52:29 +02:00
send_messages = True
if not self.next_poll_time.get(roomid, None):
self.next_poll_time[roomid] = now
2019-12-30 23:52:29 +02:00
send_messages = False
if now >= self.next_poll_time.get(roomid):
accounts = self.account_rooms[roomid]
for account in accounts:
2019-12-30 23:52:29 +02:00
try:
await self.poll_account(bot, account, roomid, send_messages)
except InstagramNotFoundException:
2020-01-02 14:27:29 +02:00
print('ig error: there is ', account,
' account that does not exist - deleting from room')
2019-12-30 23:52:29 +02:00
self.account_rooms[roomid].remove(account)
bot.save_settings()
self.first_run = False
2019-12-30 23:52:29 +02:00
async def poll_account(self, bot, account, roomid, send_messages):
print('polling', account, roomid, send_messages)
medias = self.instagram.get_medias(account, 5)
for media in medias:
2019-12-30 23:52:29 +02:00
if send_messages:
if media.identifier not in self.known_ids:
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)
polldelay = timedelta(minutes=30 + randrange(30))
self.next_poll_time[roomid] = datetime.now() + polldelay
async def matrix_message(self, bot, room, event):
args = event.body.split()
if len(args) == 2:
if args[1] == 'list':
await bot.send_text(room, f'Instagram accounts in this room: {self.account_rooms.get(room.room_id) or []}')
elif args[1] == 'poll':
bot.must_be_owner(event)
2019-12-30 23:52:29 +02:00
for roomid in self.account_rooms:
self.next_poll_time[roomid] = datetime.now()
await self.poll_all_accounts(bot)
elif args[1] == 'clear':
bot.must_be_admin(room, event)
self.account_rooms[room.room_id] = []
bot.save_settings()
await bot.send_text(room, 'Cleared all instagram accounts from this room')
if len(args) == 3:
if args[1] == 'add':
bot.must_be_admin(room, event)
account = args[2]
print(f'Adding account {account} to room id {room.room_id}')
if self.account_rooms.get(room.room_id):
if account not in self.account_rooms[room.room_id]:
self.account_rooms[room.room_id].append(account)
else:
await bot.send_text(room, 'This instagram account already added in this room!')
return
else:
self.account_rooms[room.room_id] = [account]
2020-01-02 14:27:29 +02:00
print(
f'Accounts now for this room {self.account_rooms.get(room.room_id)}')
try:
2019-12-30 23:52:29 +02:00
await self.poll_account(bot, account, room.room_id, False)
bot.save_settings()
await bot.send_text(room, 'Added new instagram account to this room')
except InstagramNotFoundException:
await bot.send_text(room, 'Account doesn\'t seem to exist')
self.account_rooms[room.room_id].remove(account)
elif args[1] == 'del':
bot.must_be_admin(room, event)
account = args[2]
2020-01-02 14:27:29 +02:00
print(
f'Removing account {account} from room id {room.room_id}')
if self.account_rooms.get(room.room_id):
self.account_rooms[room.room_id].remove(account)
2020-01-02 14:27:29 +02:00
print(
f'Accounts now for this room {self.account_rooms.get(room.room_id)}')
bot.save_settings()
await bot.send_text(room, 'Removed instagram account from this room')
def get_settings(self):
2020-01-02 14:27:29 +02:00
return {'account_rooms': self.account_rooms}
def set_settings(self, data):
if data.get('account_rooms'):
self.account_rooms = data['account_rooms']
def help(self):
return('Instagram polling')