forked from tmhlsky/Instagram-API-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_download.py
71 lines (53 loc) · 1.85 KB
/
thread_download.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Use text editor to edit the script and type in valid Instagram username/password
import json
from InstagramAPI import InstagramAPI
class DownloadThread():
def __init__(self, client, thread_id):
self.client = client
self.thread = thread_id
self.newest_cursor = None
self.oldest_cursor = None
self.users = {}
self.conversation = []
def init_owner(self):
if not self.client.getProfileData():
print("Failed!\n")
user = self.client.LastJson.get('user')
self._add_user(user)
def _request(self):
return self.client.getv2Threads(thread_id, self.oldest_cursor)
def _download(self):
if self.oldest_cursor is not None:
self._request()
self._save()
def _save(self):
data = self.client.LastJson.get('thread')
self.conversation = data['items'][::-1] + self.conversation
self.oldest_cursor = data.get('oldest_cursor')
self.newest_cursor = data.get('newest_cursor')
self._download()
def add_users(self, users):
for user in users:
self._add_user(user)
def _add_user(self, user):
self.users[user['pk']] = {'full_name': user['pk'], 'username': user['username']}
def download(self):
if not self._request():
print("Failed!\n")
data = self.client.LastJson.get('thread')
self.add_users(data['users'])
self._save()
def save(self):
dump = json.dumps(self.conversation)
with open('back.txt', 'w') as f:
f.write(dump)
if __name__ == "__main__":
thread_id = '' # id thread for download
InstagramAPI = InstagramAPI("login", "password")
InstagramAPI.login()
inst = DownloadThread(InstagramAPI, thread_id)
inst.download()
inst.save()