Skip to content

Commit

Permalink
Add API method for get direct messages
Browse files Browse the repository at this point in the history
  • Loading branch information
darland committed Aug 13, 2017
1 parent c333876 commit fbf86c1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
7 changes: 7 additions & 0 deletions InstagramAPI/InstagramAPI.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ def getv2Inbox(self):
inbox = self.SendRequest('direct_v2/inbox/?')
return inbox

def getv2Threads(self, thread, cursor=None):
endpoint = 'direct_v2/threads/{0}'.format(thread)
if cursor is not None:
endpoint += '?cursor={0}'.format(cursor)
inbox = self.SendRequest(endpoint)
return inbox

def getUserTags(self, usernameId):
tags = self.SendRequest('usertags/'+ str(usernameId) +'/feed/?rank_token='+ str(self.rank_token) +'&ranked_content=true&')
return tags
Expand Down
68 changes: 68 additions & 0 deletions examples/thread_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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__":
user, pwd = '', '' # your credentials
thread_id = '' # id thread for download

InstagramAPI = InstagramAPI(user, pwd)
InstagramAPI.login()

inst = DownloadThead(InstagramAPI, thread_id)
inst.download()
inst.save()

0 comments on commit fbf86c1

Please sign in to comment.