Ben 11 месяцев назад
Родитель
Сommit
cd26ec447c
1 измененных файлов с 10 добавлено и 6 удалено
  1. 10 6
      app.py

+ 10 - 6
app.py

@@ -45,7 +45,7 @@ def p():
     return {"status": 0}
 
 
-def convert_dto(info):
+def convert_dto(info, region):
     thumbnails = []
     for item in info.get("thumbnails", []):
         if item.get("width"):
@@ -60,6 +60,9 @@ def convert_dto(info):
                 "acodec") != "none" and item.get("vcodec"):
             format_id = item.get("format_id")
             if format_id:
+                url = item.get("url")
+                if region != "US":
+                    url = f"http://d2rueqse8qhuiu.cloudfront.net/p?videoId={info['display_id']}&formatId={format_id}",
                 formats.append({
                     "width": f"{item.get('width', 0)}",
                     "height": f"{item.get('height', 0)}",
@@ -68,8 +71,7 @@ def convert_dto(info):
                     "itag": 0,
                     "fps": "0",
                     "bitrate": "0",
-                    "url": f"http://d2rueqse8qhuiu.cloudfront.net/p?videoId={info['display_id']}&formatId={format_id}",
-                    # "url": item.get("url"),
+                    "url": url,
                     "ext": item.get("ext"),
                     "vcodec": item.get("vcodec", ""),
                     "acodec": item.get("acodec", ""),
@@ -106,12 +108,14 @@ def convert_dto(info):
 
 @app.route("/extract", methods=["GET", "POST"])
 def extract():
-    ip = request.headers.get("X Forwarded-For")
-    logging.info(f"ip: {ip}")
+    ip = request.headers.get("X-Forwarded-For")
+    response = requests.get(f"https://ipinfo.ipidea.io/?ip={ip}").json()
+    logging.info(f"ip: {ip}, response: {response}")
+    region = response.get("country_code", "")
     url: str = request.json.get("url")
     logging.info(f"url: {url}")
     info = fetch_info(url=url)
-    return convert_dto(info=info)
+    return convert_dto(info=info, region=region)
 
 
 @app.route("/health", methods=["GET"])