-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·106 lines (84 loc) · 3.18 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
#!/bin/python
from requests import Session
from argparse import ArgumentParser
from json import loads as to_json
from uuid import uuid4
from html import unescape
from re import findall
import ffmpeg
from os import remove
def load_cookies(opts):
with open(opts.c, "r") as fd:
temp = fd.read()
temp = to_json(temp)
opts.cook = {}
for cc in temp:
opts.cook[cc["name"]] = cc["value"]
def set_shortcode(opts):
temp = opts.url[8:] # Assuming it starts with https://
opts.shortcode = temp.split("/")[2]
def find_urls(data):
def only_url(tag: str):
start = tag.find(">")
end = tag.find("<", start)
return unescape(tag[start + 1:end])
found = findall("<BaseURL .*>.*</BaseURL>", data)
return list(map(only_url, found))
def generate_video(dash, sess):
parsed = find_urls(dash)
base = uuid4()
with open(f"temp_{base}.mp4", "wb") as fd:
fd.write(sess.get(parsed[0]).content)
with open(f"temp_{base}.mp3", "wb") as fd:
fd.write(sess.get(parsed[-1]).content)
vid = ffmpeg.input(f"temp_{base}.mp4")
aud = ffmpeg.input(f"temp_{base}.mp3")
ffmpeg.output(vid, aud, f"{base}.mp4").run()
remove(f"temp_{base}.mp4")
remove(f"temp_{base}.mp3")
def main(opts):
sess = Session()
sess.get(opts.url)
cookies = opts.cook
headers = {
'authority': 'www.instagram.com',
'accept': '*/*',
'accept-language': 'en-US,en;q=0.8',
'content-type': 'application/x-www-form-urlencoded',
'origin': 'https://www.instagram.com',
'sec-ch-ua': '"Brave";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Linux"',
'sec-ch-ua-platform-version': '"6.3.0"',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
'sec-gpc': '1',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',
}
data = {
'fb_dtsg': 'NAcPrYSUpWHkA9KixRKazAVxM8SyIp2p07m5bKB13ymUUxVXUKCF-JA:17864642926059691:1683851979',
'fb_api_caller_class': 'RelayModern',
'variables': '{"shortcode": "' + opts.shortcode + '"}',
'doc_id': '6129119620513740',
}
response = sess.post('https://www.instagram.com/api/graphql', cookies=cookies, headers=headers, data=data)
data = response.json()
dash = data["data"]["xdt_api__v1__media__shortcode__web_info"]["items"][0]["video_dash_manifest"]
if dash is not None:
generate_video(dash, sess)
carous = data["data"]["xdt_api__v1__media__shortcode__web_info"]["items"][0]["carousel_media"]
if carous is None:
return
for item in carous:
if item["video_dash_manifest"] is not None:
generate_video(item["video_dash_manifest"], sess)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument("-c", help="Cookie file", type=str, required=True)
parser.add_argument("url", help="URL of a post/reel")
opts = parser.parse_args()
if opts.c:
load_cookies(opts)
set_shortcode(opts)
main(opts)