From 687194a0ffbc92afd6f7bd8ed481e469a59bfeb4 Mon Sep 17 00:00:00 2001 From: Ville Ranki Date: Wed, 25 Dec 2019 23:58:36 +0200 Subject: [PATCH] Added instagram module, some finetuning --- Pipfile | 1 + README.md | 17 +++++++++- bot.py | 1 - modules/ig.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++ modules/teamup.py | 1 + 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 modules/ig.py diff --git a/Pipfile b/Pipfile index cd8cb38..c42a3df 100644 --- a/Pipfile +++ b/Pipfile @@ -12,6 +12,7 @@ google-api-python-client = "*" google-auth-httplib2 = "*" google-auth-oauthlib = "*" requests = "*" +igramscraper = "*" [dev-packages] pylint = "*" diff --git a/README.md b/README.md index f009923..e18678a 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ Commands: * !teamup add [calendar id] - add calendar to this room (Must be done as room admin) * !teamup del [calendar id] - delete calendar from this room (Must be done as room admin) * !teamup list - list calendars in this room -* !teamup poll - poll now for changes +* !teamup poll - poll now for changes (Must be done as bot owner) ### Google Calendar @@ -113,6 +113,21 @@ Example: * !loc Tampere +### Instagram + +Polls instagram account(s) and posts new items to the room. Uses instagram scraper library +without any authentication or api key so it polls only every hour to keep traffic at minimum. + +See: https://github.com/realsirjoe/instagram-scraper/ + +Commands: + +* !ig add [accountname] - Add instagram account to this room (Must be done as room admin) +* !ig del [accountname] - Delete instagram account from room (Must be done as room admin) +* !ig list - List accounts in room +* !ig poll - Poll for new items (Must be done as bot owner) + + ## Bot setup * Create a Matrix user diff --git a/bot.py b/bot.py index 952ab28..6982a4b 100755 --- a/bot.py +++ b/bot.py @@ -61,7 +61,6 @@ class Bot: # Returns true if event's sender is admin in the room event was sent in def is_admin(self, room, event): - print(room.power_levels) if not event.sender in room.power_levels.users: return False return room.power_levels.users[event.sender] >= 50 diff --git a/modules/ig.py b/modules/ig.py new file mode 100644 index 0000000..7d9d093 --- /dev/null +++ b/modules/ig.py @@ -0,0 +1,84 @@ +from igramscraper.instagram import Instagram + +class MatrixModule: + instagram = Instagram() + + known_ids = set() + first_run = True + account_rooms = dict() # Roomid -> [account, account..] + + async def matrix_poll(self, bot, pollcount): + if len(self.account_rooms): + if pollcount % (6 * 60) == 0: # Poll every 60 min automatically + await self.poll_all_accounts(bot) + + async def poll_all_accounts(self, bot): + for roomid in self.account_rooms: + accounts = self.account_rooms[roomid] + for account in accounts: + await self.poll_account(bot, account, roomid) + + self.first_run = False + + async def poll_account(self, bot, account, roomid): + medias = self.instagram.get_medias(account, 5) + + for media in medias: + if not self.first_run: + if not media.identifier in self.known_ids: + await bot.send_html(bot.get_room_by_id(roomid), f'Instagram {account}: {media.caption}', media.caption) + + self.known_ids.add(media.identifier) + + 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) + await self.poll_all_accounts(bot) + 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] + + print(f'Accounts now for this room {self.account_rooms.get(room.room_id)}') + + bot.save_settings() + await bot.send_text(room, 'Added new instagram account to this room') + if args[1] == 'del': + bot.must_be_admin(room, event) + + account = args[2] + 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) + + 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): + 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') diff --git a/modules/teamup.py b/modules/teamup.py index 50e2fa6..1a68a25 100644 --- a/modules/teamup.py +++ b/modules/teamup.py @@ -33,6 +33,7 @@ class MatrixModule: if args[1] == 'list': await bot.send_text(room, f'Calendars in this room: {self.calendar_rooms.get(room.room_id) or []}') elif args[1] == 'poll': + bot.must_be_owner(event) await self.poll_all_calendars(bot) elif len(args) == 3: if args[1] == 'add':