ig: added clear command, random intervals and other tuning.

This commit is contained in:
Ville Ranki 2019-12-30 23:16:39 +02:00
parent 13cd238a22
commit 19057ec0c7
2 changed files with 32 additions and 13 deletions

View File

@ -118,7 +118,7 @@ Example:
### Instagram ### Instagram
Polls instagram account(s) and posts new items to the room. Uses instagram scraper library 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. without any authentication or api key so it polls only randomly every 30 to 60 minutes to keep traffic at minimum.
See: https://github.com/realsirjoe/instagram-scraper/ See: https://github.com/realsirjoe/instagram-scraper/
@ -128,7 +128,7 @@ Commands:
* !ig del [accountname] - Delete instagram account from 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 list - List accounts in room
* !ig poll - Poll for new items (Must be done as bot owner) * !ig poll - Poll for new items (Must be done as bot owner)
* !ig clear - Clear all ig accounts from this room (Must be done as room admin)
## Bot setup ## Bot setup
@ -171,7 +171,7 @@ docker-compose up
## Env variables ## Env variables
User, access token and server should be self-explanatory. Set JOIN_ON_INVITE to anything if you want the bot to User, access token and server should be self-explanatory. Set JOIN_ON_INVITE to anything if you want the bot to
join invites automatically. join invites automatically (do not set it if you don't want it to join).
You can set MATRIX_PASSWORD if you want to get access token. Normally you can use Riot to get it. You can set MATRIX_PASSWORD if you want to get access token. Normally you can use Riot to get it.

View File

@ -1,4 +1,7 @@
from igramscraper.instagram import Instagram from igramscraper.instagram import Instagram
from igramscraper.exception.instagram_not_found_exception import InstagramNotFoundException
from datetime import datetime, timedelta
from random import randrange
class MatrixModule: class MatrixModule:
instagram = Instagram() instagram = Instagram()
@ -6,17 +9,21 @@ class MatrixModule:
known_ids = set() known_ids = set()
first_run = True first_run = True
account_rooms = dict() # Roomid -> [account, account..] account_rooms = dict() # Roomid -> [account, account..]
next_poll_time = dict() # Roomid -> datetime
async def matrix_poll(self, bot, pollcount): async def matrix_poll(self, bot, pollcount):
if len(self.account_rooms): if len(self.account_rooms):
if pollcount % (6 * 60) == 0: # Poll every 60 min automatically await self.poll_all_accounts(bot)
await self.poll_all_accounts(bot)
async def poll_all_accounts(self, bot): async def poll_all_accounts(self, bot):
now = datetime.now()
for roomid in self.account_rooms: for roomid in self.account_rooms:
accounts = self.account_rooms[roomid] if not self.next_poll_time.get(roomid, None):
for account in accounts: self.next_poll_time[roomid] = now
await self.poll_account(bot, account, roomid) if now >= self.next_poll_time.get(roomid):
accounts = self.account_rooms[roomid]
for account in accounts:
await self.poll_account(bot, account, roomid)
self.first_run = False self.first_run = False
@ -25,10 +32,11 @@ class MatrixModule:
for media in medias: for media in medias:
if not self.first_run: if not self.first_run:
if not media.identifier in self.known_ids: 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}') 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) 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): async def matrix_message(self, bot, room, event):
args = event.body.split() args = event.body.split()
@ -39,6 +47,11 @@ class MatrixModule:
elif args[1] == 'poll': elif args[1] == 'poll':
bot.must_be_owner(event) bot.must_be_owner(event)
await self.poll_all_accounts(bot) 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 len(args) == 3:
if args[1] == 'add': if args[1] == 'add':
bot.must_be_admin(room, event) bot.must_be_admin(room, event)
@ -57,9 +70,15 @@ class MatrixModule:
print(f'Accounts now for this room {self.account_rooms.get(room.room_id)}') print(f'Accounts now for this room {self.account_rooms.get(room.room_id)}')
bot.save_settings() try:
await bot.send_text(room, 'Added new instagram account to this room') await self.poll_account(bot, account, room.room_id)
if args[1] == 'del': 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) bot.must_be_admin(room, event)
account = args[2] account = args[2]