<filename>api/utils/statistic.py<gh_stars>0
import re

import requests

from api.utils.logger import logger


class Statistics:

    def __init__(self):
        self._flag_domain = "client.wegather.world"
        self._hm_js_url = "https://hm.baidu.com/hm.js?9f472f02c404ee99590fa4402c1af609"
        self._hm_status_url = "https://hm.baidu.com/hm.gif"

    def get_hm_js(self, localhost: str, cookies: dict) -> str:
        cookies_str = ""
        for key, value in cookies.items():
            cookies_str += f"{key}={value};"

        stat_headers = {
            "Referer": self._flag_domain,
            "Cookie": cookies_str,  # chrome blocked
        }
        logger.debug(stat_headers)
        resp = requests.get(self._hm_js_url, headers=stat_headers)
        if resp.status_code != 200:
            return ""
        localhost = localhost.replace("http://", "")  # ip:host
        text = resp.text.replace("https", "http") \
            .replace("hm.baidu.com/hm.gif", localhost + "/statistics") \
            .replace("hm.baidu.com", localhost) \
            .replace(f"{self._flag_domain}/statistics", localhost + "/statistics")
        return text

    def transmit(self, request):
        """百度统计转发"""
        args = dict(request.args)
        ref_page_u = args.get("u", "")  # u = (file|http):///path/to/index.html#/
        ref_page_su = args.get("su", "")
        pat = re.compile(r".+?:///.+?/index\.html#(?P<route>/[^/]+).*")  # route= index|detail|tvlive|result
        args["u"] = pat.sub(rf"{self._flag_domain}\g<route>", ref_page_u)  # 替换 index 文件路径为标记域名 host/route
        args["su"] = pat.sub(rf"{self._flag_domain}\g<route>", ref_page_su)

        cookies_str = ""
        for key, value in request.cookies.items():
            cookies_str += f"{key}={value};"

        stat_headers = {
            "User-Agent": request.headers.get("User-Agent"),
            "Referer": args["u"] or self._flag_domain,
            "Cookie": cookies_str,
        }
        logger.debug(args)
        logger.debug(stat_headers)
        resp = requests.get(self._hm_status_url, params=args, headers=stat_headers)
        return resp.content

<filename>api/utils/statistic.py<gh_stars>0
import re

import requests

from api.utils.logger import logger


class Statistics:

    def __init__(self):
        self._flag_domain = "client.wegather.world"
        self._hm_js_url = "https://hm.baidu.com/hm.js?9f472f02c404ee99590fa4402c1af609"
        self._hm_status_url = "https://hm.baidu.com/hm.gif"

    def get_hm_js(self, localhost: str, cookies: dict) -> str:
        cookies_str = ""
        for key, value in cookies.items():
            cookies_str += f"{key}={value};"

        stat_headers = {
            "Referer": self._flag_domain,
            "Cookie": cookies_str,  # chrome blocked
        }
        logger.debug(stat_headers)
        resp = requests.get(self._hm_js_url, headers=stat_headers)
        if resp.status_code != 200:
            return ""
        localhost = localhost.replace("http://", "")  # ip:host
        text = resp.text.replace("https", "http") \
            .replace("hm.baidu.com/hm.gif", localhost + "/statistics") \
            .replace("hm.baidu.com", localhost) \
            .replace(f"{self._flag_domain}/statistics", localhost + "/statistics")
        return text

    def transmit(self, request):
        """百度统计转发"""
        args = dict(request.args)
        ref_page_u = args.get("u", "")  # u = (file|http):///path/to/index.html#/
        ref_page_su = args.get("su", "")
        pat = re.compile(r".+?:///.+?/index\.html#(?P<route>/[^/]+).*")  # route= index|detail|tvlive|result
        args["u"] = pat.sub(rf"{self._flag_domain}\g<route>", ref_page_u)  # 替换 index 文件路径为标记域名 host/route
        args["su"] = pat.sub(rf"{self._flag_domain}\g<route>", ref_page_su)

        cookies_str = ""
        for key, value in request.cookies.items():
            cookies_str += f"{key}={value};"

        stat_headers = {
            "User-Agent": request.headers.get("User-Agent"),
            "Referer": args["u"] or self._flag_domain,
            "Cookie": cookies_str,
        }
        logger.debug(args)
        logger.debug(stat_headers)
        resp = requests.get(self._hm_status_url, params=args, headers=stat_headers)
        return resp.content

# chrome blocked # ip:host 百度统计转发 # u = (file|http):///path/to/index.html#/ #(?P<route>/[^/]+).*")  # route= index|detail|tvlive|result # 替换 index 文件路径为标记域名 host/route