-
Notifications
You must be signed in to change notification settings - Fork 3
/
vsco_downloader.py
152 lines (132 loc) · 5.14 KB
/
vsco_downloader.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
#!/usr/bin/env python3
# VSCO DOWNLOADER
# 2021 (c) Micha Johannes Birklbauer
# https://github.com/michabirklbauer/
version = "1.0.3"
date = "20221230"
import urllib.request as ur
import traceback as tb
import json
import sys
import os
import re
def download(vsco_media_url, get_video_thumbnails = True, save = True):
request_header = { "User-Agent" : "Mozilla/5.0 (Windows NT 6.1; Win64; x64)" }
request = ur.Request(vsco_media_url, headers = request_header)
data = ur.urlopen(request).read()
data_cleaned_1 = str(data).split("<script>window.__PRELOADED_STATE__ =")[1]
data_cleaned_2 = str(data_cleaned_1).split("</script>")[0]
data_cleaned_3 = str(data_cleaned_2).strip()
data_cleaned_4 = str(data_cleaned_3).replace("\\x", "\\u00")
data_cleaned_5 = re.sub(r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})', r'', data_cleaned_4)
try:
json_data = json.loads(data_cleaned_5)
except Exception as e:
print("ERROR: Failed to load json data!")
tb.print_exc()
return 1
opener = ur.build_opener()
opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64)")]
ur.install_opener(opener)
media_urls = []
try:
medias = json_data["medias"]["byId"]
for media in medias:
info = medias[media]["media"]
if not bool(info["isVideo"]) or get_video_thumbnails:
media_url = "https://" + str(info["responsiveUrl"].encode().decode("unicode-escape"))
media_name = str(media) + ".jpg"
if save:
ur.urlretrieve(media_url, media_name)
media_urls.append(media_url)
if bool(info["isVideo"]):
media_url = "https://" + str(info["videoUrl"].encode().decode("unicode-escape"))
media_name = str(media) + ".mp4"
if save:
ur.urlretrieve(media_url, media_name)
media_urls.append(media_url)
except Exception as e:
print("ERROR: Failed to extract image/video location!")
tb.print_exc()
return 1
return media_urls
def vsco_downloader(option = None, arg = None):
if option == 1 or option == None:
link = "https://vsco.co/emilieristevski/media/561f648001146426743090fa"
prompt_str = "Please enter an URL to an VSCO post e. g. " + link + " \n"
url = input(prompt_str)
r = download(url)
if r != 1:
print("Download successfully!")
return 0
else:
print("ERROR encountered: Download may have failed!")
return 1
if option == 2 or option == "url":
if arg == None:
print("ERROR: No URL provided. Exiting!")
return 1
r = download(arg)
if r != 1:
print("Download successfully!")
return 0
else:
print("ERROR encountered: Download may have failed!")
return 1
if option == 3 or option == "file":
if arg == None:
print("ERROR: No file provided. Exiting!")
return 1
with open(arg, "r") as f:
lines = f.readlines()
f.close()
count = int(len(lines))
counter = 1
percent = [ \
"[--------------------]", "[#-------------------]",
"[##------------------]", "[###-----------------]",
"[####----------------]", "[#####---------------]",
"[######--------------]", "[#######-------------]",
"[########------------]", "[#########-----------]",
"[##########----------]", "[###########---------]",
"[############--------]", "[#############-------]",
"[##############------]", "[###############-----]",
"[################----]", "[#################---]",
"[##################--]", "[###################-]",
"[####################]"]
r = 0
for line in lines:
l = line.lstrip().rstrip()
r_ = download(l)
status = counter/count
status_bar = percent[int(status * 20)]
status_msg = "Downloaded " + str(line) + "\n" + \
"Download at " + str(status*100) + "% \n" + \
status_bar + "\n"
counter = counter + 1
print(status_msg)
if r_ == 1:
r = 1
if r == 0:
print("Downloaded all Posts successfully!")
return 0
else:
print("ERROR encountered: Downloads of one or more posts may have failed!")
return 1
if option not in [1, 2, 3, None, "url", "file"]:
print("ERROR: Unrecognized option. Exiting!")
return 1
if __name__ == '__main__':
if len(sys.argv) == 1:
result = vsco_downloader()
print(result)
elif len(sys.argv) == 2:
if not os.path.isfile(sys.argv[1]):
result = vsco_downloader("url", sys.argv[1])
print(result)
else:
result = vsco_downloader("file", sys.argv[1])
print(result)
else:
print("Wrong usage! Try running without parameters or read documentation!")