-
Notifications
You must be signed in to change notification settings - Fork 13
/
instagram.py
58 lines (49 loc) · 1.59 KB
/
instagram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import time
import requests
import config
def new_media(bot):
rs = requests.Session()
for insta_conf in config.bot.instagram:
r = rs.get('https://graph.instagram.com/me/media', params={
'fields': 'username,caption,timestamp,permalink,media_type,media_url,thumbnail_url',
'access_token': insta_conf['token'],
})
r.raise_for_status()
data = r.json()['data']
if not data:
continue
data.sort(key=lambda media: media['timestamp'])
last_timestamp = config.state.instagram.get(insta_conf['user_id'])
if last_timestamp is None:
# never seen this account before; post only the most recent image
post_media(bot, insta_conf['channels'], data[-1])
else:
for media in data:
if media['timestamp'] <= last_timestamp:
continue
post_media(bot, insta_conf['channels'], media)
time.sleep(2)
new_last_timestamp = data[-1]['timestamp']
if last_timestamp is None or new_last_timestamp > last_timestamp:
config.state.instagram[insta_conf['user_id']] = new_last_timestamp
config.state.save()
def post_media(bot, channel_ids, media):
embed = {
'description': media['caption'],
'url': media['permalink'],
'author': {
'name': media['username'],
'url': 'https://www.instagram.com/%s/' % media['username'],
},
}
if media['media_type'] == 'IMAGE':
embed['image'] = {'url': media['media_url']}
elif media['media_type'] == 'VIDEO':
embed['thumbnail'] = {'url': media['thumbnail_url']}
for channel_id in channel_ids:
bot.send_message(channel_id, '<%s>' % media['permalink'], embed)
def main():
import bot
new_media(bot.Bot({}))
if __name__ == '__main__':
main()