|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +''' |
| 4 | +@Description:Server.py |
| 5 | +@Date :2023/02/25 17:03:32 |
| 6 | +@Author :JohnserfSeed |
| 7 | +@version :0.0.1 |
| 8 | +@License :MIT License |
| 9 | +@Github :https://github.com/johnserf-seed |
| 10 | +@Mail :johnserf-seed@foxmail.com |
| 11 | +------------------------------------------------- |
| 12 | +Change Log : |
| 13 | +2023/02/25 17:03:32 - Create Flask Server XB Gen |
| 14 | +2023/08/03 16:48:34 - Fix ttwid |
| 15 | +------------------------------------------------- |
| 16 | +''' |
| 17 | + |
| 18 | +import time |
| 19 | +import execjs |
| 20 | +# import sqlite3 |
| 21 | +import requests |
| 22 | + |
| 23 | +from flask import Flask |
| 24 | +from flask import request |
| 25 | +from flask import jsonify |
| 26 | +# from flask import make_response |
| 27 | +# from flask import render_template |
| 28 | + |
| 29 | +from urllib.parse import urlencode |
| 30 | +from urllib.parse import unquote |
| 31 | +from urllib.parse import parse_qsl |
| 32 | + |
| 33 | +class Server: |
| 34 | + def __init__(self) -> None: |
| 35 | + # 工厂模式 |
| 36 | + self.app = Flask(__name__) |
| 37 | + |
| 38 | + self.app.config.from_mapping( |
| 39 | + SECRET_KEY='douyin-xbogus' |
| 40 | + ) |
| 41 | + |
| 42 | + self.app.config['JSON_AS_ASCII'] = False |
| 43 | + |
| 44 | + with open("x-bogus.js", "r", encoding="utf-8") as fp: |
| 45 | + self.xbogust_func = execjs.compile(fp.read()) |
| 46 | + |
| 47 | + with open("x-tt-params.js", "r", encoding="utf-8") as fp: |
| 48 | + self.xttm_func = execjs.compile(fp.read()) |
| 49 | + |
| 50 | + self.ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36" |
| 51 | + |
| 52 | + # 获取xg参数 |
| 53 | + def getXG(self, url_path, params): |
| 54 | + xbogus = self.xbogust_func.call("getXB", url_path) |
| 55 | + # 字典中添加xg |
| 56 | + params["X-Bogus"] = xbogus |
| 57 | + tips = { |
| 58 | + "status_code": "200", |
| 59 | + "time": { |
| 60 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 61 | + "timestamp": int(round(time.time() * 1000)) |
| 62 | + }, |
| 63 | + "result": [{ |
| 64 | + "params": params, |
| 65 | + "paramsencode": urlencode(params, safe="="), |
| 66 | + "user-agent": self.ua, |
| 67 | + "X-Bogus": { |
| 68 | + 0: xbogus, |
| 69 | + 1: "X-Bogus=%s" % xbogus |
| 70 | + } |
| 71 | + }] |
| 72 | + } |
| 73 | + print(tips) |
| 74 | + return jsonify(tips) |
| 75 | + |
| 76 | + # 生成x-tt-params |
| 77 | + def getxttparams(self, url_path): |
| 78 | + xttp = self.xttm_func.call("getXTTP", url_path) |
| 79 | + tips = { |
| 80 | + "status_code": "200", |
| 81 | + "time": { |
| 82 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 83 | + "timestamp": int(round(time.time() * 1000)) |
| 84 | + }, |
| 85 | + "result": [{ |
| 86 | + "headers": { |
| 87 | + "user-agent": self.ua, |
| 88 | + "x-tt-params": xttp |
| 89 | + } |
| 90 | + }] |
| 91 | + } |
| 92 | + print(tips) |
| 93 | + return jsonify(tips) |
| 94 | + |
| 95 | + def gen_ttwid(self) -> str: |
| 96 | + """生成请求必带的ttwid |
| 97 | + param :None |
| 98 | + return:ttwid |
| 99 | + """ |
| 100 | + url = 'https://ttwid.bytedance.com/ttwid/union/register/' |
| 101 | + data = '{"region":"cn","aid":1768,"needFid":false,"service":"www.ixigua.com","migrate_info":{"ticket":"","source":"node"},"cbUrlProtocol":"https","union":true}' |
| 102 | + response = requests.request("POST", url, data=data) |
| 103 | + # j = ttwid k = 1%7CfPx9ZM..... |
| 104 | + for j, k in response.cookies.items(): |
| 105 | + tips = { |
| 106 | + "status_code": "200", |
| 107 | + "time": { |
| 108 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 109 | + "timestamp": int(round(time.time() * 1000)) |
| 110 | + }, |
| 111 | + "result": [{ |
| 112 | + "headers": { |
| 113 | + "user-agent": self.ua, |
| 114 | + "cookie": "ttwid=%s;" % k |
| 115 | + } |
| 116 | + }] |
| 117 | + } |
| 118 | + print(tips) |
| 119 | + return jsonify(tips) |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == "__main__": |
| 123 | + server = Server() |
| 124 | + # 首页 |
| 125 | + @server.app.route('/', methods=['GET', 'POST']) |
| 126 | + def index(): |
| 127 | + tips = { |
| 128 | + "status_code": "-1", |
| 129 | + "time": { |
| 130 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 131 | + "timestamp": int(round(time.time() * 1000)) |
| 132 | + }, |
| 133 | + "path": { |
| 134 | + 0: "/xg/path/?url=", |
| 135 | + 2: "/x-tt-params/path" |
| 136 | + } |
| 137 | + } |
| 138 | + print(tips) |
| 139 | + return jsonify(tips) |
| 140 | + |
| 141 | + # xg参数 |
| 142 | + @server.app.route('/xg/path/', methods=['GET', 'POST']) |
| 143 | + def xgpath(): |
| 144 | + path = request.args.get('url', '') |
| 145 | + # 如果str路径为空 |
| 146 | + if not path: |
| 147 | + tips = { |
| 148 | + "status_code": "-3", |
| 149 | + "time": { |
| 150 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 151 | + "timestamp": int(round(time.time() * 1000)) |
| 152 | + }, |
| 153 | + "message": { |
| 154 | + 0: "The key url cannot be empty and the need for url encoding, The '&' sign needs to be escaped to '%26', Use urllib.parse.quote(url) to escape. Example:/xg/path/?url=aid=6383%26sec_user_id=xxx%26max_cursor=0%26count=10", |
| 155 | + 1: "url参数不能为空,且需要注意传入值中的“&”需要转义成“%26”,使用urllib.parse.quote(url)转义. 例如:/xg/path/?url=aid=6383%26sec_user_id=xxx%26max_cursor=0%26count=10" |
| 156 | + } |
| 157 | + } |
| 158 | + print(tips) |
| 159 | + return jsonify(tips) |
| 160 | + else: |
| 161 | + # url转字典 |
| 162 | + params = dict(parse_qsl(path)) |
| 163 | + # 字典转url |
| 164 | + url_path = urlencode(params, safe="=") |
| 165 | + return server.getXG(url_path, params) |
| 166 | + |
| 167 | + # x-tt-params参数 |
| 168 | + @server.app.route('/x-tt-params/path', methods=['GET', 'POST']) |
| 169 | + def xttppath(): |
| 170 | + try: |
| 171 | + path = request.json |
| 172 | + except: |
| 173 | + pass |
| 174 | + if not path: |
| 175 | + tips = { |
| 176 | + "status_code": "-5", |
| 177 | + "time": { |
| 178 | + "strftime": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()), |
| 179 | + "timestamp": int(round(time.time() * 1000)) |
| 180 | + }, |
| 181 | + "message": { |
| 182 | + 0: "Body uses raw JSON format to pass dictionary parameters, such as %s" % '{"aid": 1988,"app_name": "tiktok_web","channel": "tiktok_web".........}', |
| 183 | + 1: "body中使用raw json格式传递字典参数,如%s" % '{"aid": 1988,"app_name": "tiktok_web","channel": "tiktok_web".........}' |
| 184 | + } |
| 185 | + } |
| 186 | + print(tips) |
| 187 | + return jsonify(tips) |
| 188 | + else: |
| 189 | + return server.getxttparams(path) |
| 190 | + |
| 191 | + # ttwid |
| 192 | + @server.app.route('/xg/ttwid', methods=['GET', 'POST']) |
| 193 | + def ttwid(): |
| 194 | + return server.gen_ttwid() |
| 195 | + |
| 196 | + |
| 197 | + server.app.run(host='0.0.0.0',port='8889') |
0 commit comments