-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
174 lines (124 loc) · 5.24 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import instagram
import asyncio
import aiohttp
import pathlib
import argparse
import yarl
from datetime import date
# hacky lazy solution for right now
def fetch_all(func, *args):
responses = []
next_id = None
counter = 0
while True:
# while true loops are scary and this number came to me in a dream
# should never reach this unless someone has more than ~150k posts
# saved, which is an external problem.
if counter == 10000:
break
data, next_id = func(*args, next_id=next_id)
responses.extend(data)
counter += 1
if not next_id:
break
return responses
def list_collections(collections):
for _, c in enumerate(collections):
if c.type == "ALL_MEDIA_AUTO_COLLECTION":
print(f" - {c.name:<12} {c.size:>7} posts{'':<5} (-)")
else:
print(f" - {c.name:<12} {c.size:>7} posts{'':<5} ({c.id})")
async def download_collection(
session: instagram.Session, c: instagram.Collection, output: pathlib.Path, resume=None):
# would it be insane to use fetch_all here?
print(f"[+] Downloading collection {c.name} ({c.id})")
more_available = True
index = 0
if resume is not None:
rid, index, next_id = resume.split(":")
index = int(index)
if rid != c.id:
print(f"[!] next_id does not correspond to this collection ({c.id})")
return
print(f"[!] Starting from next_id={next_id}")
while more_available:
posts, next_id = instagram.fetch_collection_posts(session, c, next_id)
if not next_id:
more_available = False
_id_cache_write(f"{c.id}:{index}:{next_id}")
tasks = []
for p in posts:
targets = list(zip(p.get_filenames(), p.get_urls()))
for t in targets:
coro = download_file(t[1], t[0], index, verbose=False, path=output)
tasks.append(asyncio.create_task(coro))
index += 1
await asyncio.gather(*tasks)
print(f" - Fetched {len(posts)} posts. Progress ({index}/{c.size})")
print("[+] Done.")
async def download_file(url, filename, index=None, verbose=False, path=None):
url = yarl.URL(url, encoded=True)
if index is not None:
filename = f"{index}_{filename}"
if path is not None:
path = pathlib.Path(path)
else:
path = pathlib.Path(".")
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
with open(path / filename, 'wb+') as file:
chunk = await response.read()
file.write(chunk)
if verbose:
print(f"[+] Finished downloading {path / filename}")
def _id_cache_write(next_id: str):
with open(".id_cache", "w+") as cache:
cache.write(next_id)
async def main():
collections = fetch_all(instagram.fetch_collections, session)
if not args.collections:
# download everything
# index 0 should be "All Posts", but if you want to be absolutely certain
# iterate through the collections until id == "ALL_MEDIA_AUTO_COLLECTION"
for c in collections:
if c.id == "ALL_MEDIA_AUTO_COLLECTION":
await download_collection(session, c, output_dir, resume=args.resume)
break
else:
targets = [c for c in collections if c.id in args.collections]
if not targets:
print("No collection(s) found matching the supplied id(s)")
for target in targets:
await download_collection(session, target, output_dir, resume=args.resume)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-l", "--login", nargs=2,
help="attempt to login with \"username password\"")
parser.add_argument("-s", "--session", help="set `sessionid` cookie")
parser.add_argument("-o", "--output-dir", default="./saved-posts/",
help="directory to save posts")
parser.add_argument("-c", "--list-collections", action="store_true",
help="list all collections belonging to an account")
parser.add_argument("-d", nargs="*", dest="collections",
help="ids of collections to download from " \
"(if no ids are provided the script will attempt to download posts from all collections)")
parser.add_argument("-r", "--resume", help="Begin downloading from a collection at the given next_id")
args = parser.parse_args()
if args.login:
session = instagram.Session(username=args.login[0], password=args.login[1])
elif args.session:
session = instagram.Session(token=args.session)
else:
parser.print_help()
exit()
output_dir = pathlib.Path(args.output_dir) / str(date.today())
output_dir.mkdir(exist_ok=True, parents=True)
if args.list_collections:
print("[+] Collection(s) owned by current account\n")
collections = fetch_all(instagram.fetch_collections, session)
list_collections(collections)
elif args.collections is not None:
asyncio.run(main())
else:
parser.print_help()
exit()