-
Notifications
You must be signed in to change notification settings - Fork 10
/
threads.py
65 lines (49 loc) · 1.66 KB
/
threads.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
# -*- coding: utf-8 -*-
import logging
import argparse
import time
from instagram import Instagram
class Application:
def __init__(self, username, password):
self.username = username
self.password = password
self.instagram = Instagram(username, password)
self.logged_in = False
def exit_application(self):
if self.logged_in:
self.instagram.logout()
def thread_list(self, sleep_time=2):
next_page = ''
while True:
time.sleep(sleep_time)
direct = self.instagram.direct_list(next_page=next_page)
if direct:
items = direct['inbox']['threads']
for item in items:
yield item['thread_title']
if not direct['inbox']['has_older']:
return
next_page = direct['inbox']['oldest_cursor']
def run(self):
if not self.instagram.login():
self.exit_application()
return
self.logged_in = True
for thread_name in self.thread_list():
print thread_name
if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument("-u", "--username", required=True,
help="Instagram username")
ap.add_argument("-p", "--password", required=True,
help="Instagram password")
args = vars(ap.parse_args())
app = Application(args['username'], args['password'])
try:
app.run()
except KeyboardInterrupt:
app.exit_application()
except BaseException as err:
app.exit_application()
logging.error(err.message)
app.exit_application()