Skip to content

Commit

Permalink
Showing 1 changed file with 116 additions and 28 deletions.
144 changes: 116 additions & 28 deletions Util/Cookies.py
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
Expand All @@ -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

0 comments on commit 82eaf0b

Please sign in to comment.