Added instagram module, some finetuning
This commit is contained in:
parent
4017a53e5b
commit
687194a0ff
1
Pipfile
1
Pipfile
|
@ -12,6 +12,7 @@ google-api-python-client = "*"
|
||||||
google-auth-httplib2 = "*"
|
google-auth-httplib2 = "*"
|
||||||
google-auth-oauthlib = "*"
|
google-auth-oauthlib = "*"
|
||||||
requests = "*"
|
requests = "*"
|
||||||
|
igramscraper = "*"
|
||||||
|
|
||||||
[dev-packages]
|
[dev-packages]
|
||||||
pylint = "*"
|
pylint = "*"
|
||||||
|
|
17
README.md
17
README.md
|
@ -52,7 +52,7 @@ Commands:
|
||||||
* !teamup add [calendar id] - add calendar to this room (Must be done as room admin)
|
* !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 del [calendar id] - delete calendar from this room (Must be done as room admin)
|
||||||
* !teamup list - list calendars in this room
|
* !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
|
### Google Calendar
|
||||||
|
|
||||||
|
@ -113,6 +113,21 @@ Example:
|
||||||
|
|
||||||
* !loc Tampere
|
* !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
|
## Bot setup
|
||||||
|
|
||||||
* Create a Matrix user
|
* Create a Matrix user
|
||||||
|
|
1
bot.py
1
bot.py
|
@ -61,7 +61,6 @@ class Bot:
|
||||||
|
|
||||||
# Returns true if event's sender is admin in the room event was sent in
|
# Returns true if event's sender is admin in the room event was sent in
|
||||||
def is_admin(self, room, event):
|
def is_admin(self, room, event):
|
||||||
print(room.power_levels)
|
|
||||||
if not event.sender in room.power_levels.users:
|
if not event.sender in room.power_levels.users:
|
||||||
return False
|
return False
|
||||||
return room.power_levels.users[event.sender] >= 50
|
return room.power_levels.users[event.sender] >= 50
|
||||||
|
|
|
@ -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'<a href="{media.link}">Instagram {account}:</a> {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')
|
|
@ -33,6 +33,7 @@ class MatrixModule:
|
||||||
if args[1] == 'list':
|
if args[1] == 'list':
|
||||||
await bot.send_text(room, f'Calendars in this room: {self.calendar_rooms.get(room.room_id) or []}')
|
await bot.send_text(room, f'Calendars in this room: {self.calendar_rooms.get(room.room_id) or []}')
|
||||||
elif args[1] == 'poll':
|
elif args[1] == 'poll':
|
||||||
|
bot.must_be_owner(event)
|
||||||
await self.poll_all_calendars(bot)
|
await self.poll_all_calendars(bot)
|
||||||
elif len(args) == 3:
|
elif len(args) == 3:
|
||||||
if args[1] == 'add':
|
if args[1] == 'add':
|
||||||
|
|
Loading…
Reference in New Issue