-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…294 #312 #320 #332 #335 #341 #343 #346 #355 #357 #360 #361 #362 #364 #365 #367 #370 #372 #374 #376 #377 #379 #380 #381 #382 #383 #393 #394 #397 #398 #399 #401 #402 #403 #404 #406 #407 #408 #410 #414 #416 #419 #420 #422 #423 #426 #431 #432 #433 #434 #435 #436 #437 #446 #448 #449 #450 #451 #452 #454 #455 #456 #567 #460 #466 #469 #470 #471 #472 #475 #476 #477 #480 #481 #483 #484 #485 1、新增verifyFp、s_v_web_id参数生成(相同参数) 2、拆分response对象的Set-Cookie字符串并拼接 #155 #191 #202 #203 #206 #220 #226 #227 #243 #267 #287 #294 #312 #320 #332 #335 #341 #343 #346 #355 #357 #360 #361 #362 #364 #365 #367 #370 #372 #374 #376 #377 #379 #380 #381 #382 #383 #393 #394 #397 #398 #399 #401 #402 #403 #404 #406 #407 #408 #410 #414 #416 #419 #420 #422 #423 #426 #431 #432 #433 #434 #435 #436 #437 #446 #448 #449 #450 #451 #452 #454 #455 #456 #567 #460 #466 #469 #470 #471 #472 #475 #476 #477 #480 #481 #483 #484 #485
- Loading branch information
1 parent
ec387a3
commit 82eaf0b
Showing
1 changed file
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import Util | ||
#!/usr/bin/env python | ||
# -*- encoding: utf-8 -*- | ||
''' | ||
@Description:Cookies.py | ||
@Date :2023/07/29 20:13:24 | ||
@Author :JohnserfSeed | ||
@version :0.0.1 | ||
@License :MIT License | ||
@Github :https://github.com/johnserf-seed | ||
@Mail :[email protected] | ||
------------------------------------------------- | ||
Change Log : | ||
2023/07/29 20:13:24 - 添加不同类型的cookies生成 | ||
2023/08/01 15:19:38 - 对response.cookies进行拆分 | ||
------------------------------------------------- | ||
''' | ||
|
||
import re | ||
import time | ||
import random | ||
import requests | ||
|
||
|
||
class Cookies: | ||
def __init__(self, conf) -> None: | ||
self.dyCookie(conf) | ||
|
||
def generate_random_str(self, randomlength=16): | ||
def __init__(self) -> None: | ||
pass | ||
|
||
def generate_random_str(self, randomlength=16) -> str: | ||
""" | ||
根据传入长度产生随机字符串 | ||
param :randomlength | ||
|
@@ -34,27 +52,97 @@ def generate_ttwid(self) -> str: | |
for j, k in response.cookies.items(): | ||
return k | ||
|
||
def dyCookie(self, conf): | ||
self.conf = conf | ||
# self.conf[3] cookie | ||
if self.conf[3] == '' or 'odin_tt' not in self.conf[3]: | ||
self.odin_tt = '324fb4ea4a89c0c05827e18a1ed9cf9bf8a17f7705fcc793fec935b637867e2a5a9b8168c885554d029919117a18ba69' | ||
self.passport_csrf_token = '2f142a9bb5db1f81f249d6fc997fe4a1' | ||
self.dyheaders = { | ||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', | ||
# 'referer': 'https://www.douyin.com/', | ||
# 获取用户数据失败就自行替换本地cookie值 | ||
'Cookie': f'msToken={self.generate_random_str(107)};ttwid={self.generate_ttwid()};odin_tt={self.odin_tt};passport_csrf_token={self.passport_csrf_token}' | ||
} | ||
return self.dyheaders | ||
else: | ||
self.dyheaders = { | ||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36', | ||
'referer': 'https://www.douyin.com/', | ||
'Cookie': self.conf[3] | ||
} | ||
return self.dyheaders | ||
|
||
|
||
if __name__ == '__main__': | ||
Cookies() | ||
def get_fp(self) -> str: | ||
""" | ||
生成verifyFp | ||
Returns: | ||
str: verifyFp | ||
""" | ||
e = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | ||
t = len(e) | ||
milliseconds = int(round(time.time() * 1000)) | ||
base36 = '' | ||
while milliseconds > 0: | ||
remainder = milliseconds % 36 | ||
if remainder < 10: | ||
base36 = str(remainder) + base36 | ||
else: | ||
base36 = chr(ord('a') + remainder - 10) + base36 | ||
milliseconds = int(milliseconds / 36) | ||
r = base36 | ||
o = [''] * 36 | ||
o[8] = o[13] = o[18] = o[23] = '_' | ||
o[14] = '4' | ||
|
||
for i in range(36): | ||
if not o[i]: | ||
n = 0 or int(random.random() * t) | ||
if i == 19: | ||
n = 3 & n | 8 | ||
o[i] = e[n] | ||
ret = "verify_" + r + "_" + ''.join(o) | ||
return ret | ||
|
||
def get_s_v_web_id(self) -> str: | ||
""" | ||
生成s_v_web_id | ||
Returns: | ||
str: s_v_web_id | ||
""" | ||
|
||
e = list("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") | ||
t = len(e) | ||
n = self.base36_encode(int(time.time()*1000)) # 生成时间戳的36进制 | ||
|
||
r = [''] * 36 | ||
r[8] = r[13] = r[18] = r[23] = "_" | ||
r[14] = "4" | ||
|
||
for i in range(36): | ||
if not r[i]: | ||
o = int(random.random() * t) | ||
r[i] = e[3 & o | 8 if i == 19 else o] | ||
|
||
return "verify_" + n + "_" + "".join(r) | ||
|
||
def base36_encode(self, number) -> str: | ||
""" | ||
转换整数为base36字符串 | ||
Returns: | ||
str: base36 string | ||
""" | ||
|
||
alphabet = '0123456789abcdefghijklmnopqrstuvwxyz' | ||
base36 = [] | ||
|
||
while number: | ||
number, i = divmod(number, 36) | ||
base36.append(alphabet[i]) | ||
|
||
return ''.join(reversed(base36)) | ||
|
||
def split_cookies(self, cookie_str:str) -> str: | ||
""" | ||
拆分Set-Cookie字符串并拼接 | ||
Args: | ||
cookie_str (str): _description_ | ||
""" | ||
|
||
# 判断是否为字符串 | ||
if not isinstance(cookie_str, str): | ||
raise TypeError("cookie_str must be str") | ||
|
||
# 拆分Set-Cookie字符串,避免错误地在expires字段的值中分割字符串。 | ||
cookies_list = re.split(', (?=[a-zA-Z])', cookie_str) | ||
|
||
# 拆分每个Cookie字符串,只获取第一个分段(即key=value部分) | ||
cookies_list = [cookie.split(';')[0] for cookie in cookies_list] | ||
|
||
# 拼接所有的Cookie | ||
cookie_str = ";".join(cookies_list) | ||
|
||
return cookie_str |