-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbrowser.py
executable file
·173 lines (140 loc) · 5.89 KB
/
browser.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
import asyncio
import pyppeteer
import random
import time
import json
import string
import atexit
import requests
import logging
class browser:
def __init__(self, url, ua, verify = '', language='en', proxy=None, find_redirect=False, api_url=None, debug=False):
self.url = url
self.debug = debug
self.proxy = proxy
self.api_url = api_url
self.referrer = "https://www.tiktok.com/"
self.language = language
self.verify = verify
self.userAgent = ua #"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.0 Safari/537.36)"
self.args = [
"--no-sandbox",
"--disable-setuid-sandbox",
"--disable-infobars",
"--window-position=0,0",
"--ignore-certifcate-errors",
"--ignore-certifcate-errors-spki-list",
"--user-agent=" + self.userAgent
]
#self.args = []
# "--user-agent=" + self.userAgent,
if proxy != None:
if "@" in proxy:
self.args.append("--proxy-server=" + proxy.split(":")[0] + "://" + proxy.split(
"://")[1].split(":")[1].split("@")[1] + ":" + proxy.split("://")[1].split(":")[2])
else:
self.args.append("--proxy-server=" + proxy)
self.options = {
'args': self.args,
'headless': True,
'ignoreHTTPSErrors': True,
'userDataDir': "./tmp",
'handleSIGINT': False,
'handleSIGTERM': False,
'handleSIGHUP': False
}
loop = asyncio.new_event_loop()
if find_redirect:
loop.run_until_complete(self.find_redirect())
else:
loop.run_until_complete(self.start())
async def start(self):
self.browser = await pyppeteer.launch(self.options)
self.page = await self.browser.newPage()
await self.page.evaluateOnNewDocument("""() => {
delete navigator.__proto__.webdriver;
}""")
# Check for user:pass proxy
if self.proxy != None:
if "@" in self.proxy:
await self.page.authenticate({
'username': self.proxy.split("://")[1].split(":")[0],
'password': self.proxy.split("://")[1].split(":")[1].split("@")[0]
})
await stealth(self.page)
# await self.page.emulate({
# 'viewport': {'width': random.randint(320, 1920), 'height': random.randint(320, 1920), },
# 'deviceScaleFactor': random.randint(1, 3),
# 'isMobile': random.random() > 0.5,
# 'hasTouch': random.random() > 0.5
# })
# might have to switch to a tiktok url if they improve security
await self.page.goto("about:blank", {
'waitUntil': "load"
})
# self.userAgent = await self.page.evaluate("""() => {return navigator.userAgent; }""")
await self.page.setUserAgent(self.userAgent)
# self.verifyFp = ''.join(random.choice(string.ascii_lowercase + string.ascii_uppercase + string.digits) for i in range(16))
await self.page.evaluate("() => { " + self.__get_js(proxy=self.proxy) + " }")
self.signature = await self.page.evaluate('''() => {
var urls = ["''' + self.url + '''"]
var token = urls.map(x => window.byted_acrawler.sign({ url: x}))
// var t = {}
// webpackJsonp.filter(x => typeof x[1]['duD4'] === "function")[0][1].duD4(null, t)
// var token = urls.map(x => t.sign({ url: x}))
return token;
}''')
if self.api_url != None:
await self.page.goto(self.url +
"&verifyFp=" + self.verifyFp +
"&_signature=" + self.signature, {
'waitUntil': "load"
})
self.data = await self.page.content()
print(self.data)
#self.data = await json.loads(self.data)
await self.browser.close()
await self.browser.close()
self.browser.process.communicate()
async def find_redirect(self):
try:
self.browser = await pyppeteer.launch(self.options)
self.page = await self.browser.newPage()
await self.page.evaluateOnNewDocument("""() => {
delete navigator.__proto__.webdriver;
}""")
# Check for user:pass proxy
if self.proxy != None:
if "@" in self.proxy:
await self.page.authenticate({
'username': self.proxy.split("://")[1].split(":")[0],
'password': self.proxy.split("://")[1].split(":")[1].split("@")[0]
})
await stealth(self.page)
# await self.page.emulate({'viewport': {
# 'width': random.randint(320, 1920),
# 'height': random.randint(320, 1920),
# 'deviceScaleFactor': random.randint(1, 3),
# 'isMobile': random.random() > 0.5,
# 'hasTouch': random.random() > 0.5
# }})
# await self.page.setUserAgent(self.userAgent)
await self.page.goto(self.url, {
'waitUntil': "load"
})
self.redirect_url = self.page.url
await self.browser.close()
self.browser.process.communicate()
except:
await self.browser.close()
self.browser.process.communicate()
def __format_proxy(self, proxy):
if proxy != None:
return {
'http': proxy,
'https': proxy
}
else:
return None
def __get_js(self, proxy=None):
return requests.get("https://sf16-muse-va.ibytedtos.com/obj/rc-web-sdk-gcs/acrawler.js", proxies=self.__format_proxy(proxy)).text