File size: 4,435 Bytes
de4925a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
631b1ce
 
de4925a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import os
import random
import regex
import requests

app = FastAPI()

BING_URL = os.getenv("BING_URL", "https://www.bing.com")
FORWARDED_IP = (
    f"13.{random.randint(104, 107)}.{random.randint(0, 255)}.{random.randint(0, 255)}"
)
HEADERS = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/png,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "en-US,en;q=0.9",
    "cache-control": "max-age=0",
    "content-type": "application/x-www-form-urlencoded",
    "referrer": "https://www.bing.com/images/create/",
    "origin": "https://www.bing.com",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63",
    "x-forwarded-for": FORWARDED_IP,
}

error_blocked_prompt = "Your prompt has been blocked by Bing. Try to change any bad words and try again."
error_being_reviewed_prompt = "Your prompt is being reviewed by Bing. Try to change any sensitive words and try again."
error_unsupported_lang = "\nthis language is currently not supported by bing"
error_noresults = "Could not get results"
error_redirect = "Redirect failed"
error_timeout = "Your request has timed out."
error_no_images = "No images"
error_bad_images = "Bad images"

auth_cookie = "1gXql_g31ilUXmlQFACLy5cwvODf9ANzbJ-vyMNlzLV3s3Dh7uAaZx12i0of4ZHfx4-xVYpqdumLMnvsWlvvhTaDrUw25w45gPcx6B3NhXg833Uq8PfhEc23pkCb5SxHKjVpAvJuHZjVYvADq-12dSyDkcEea1J0b69ytIGVer67zZl20bqwxwSVym4LDkWHnRX1CUp2UtSgtgjJM3VBt-KzWCPzqFxa7dYmjjNPVmIs"
auth_cookie_SRCHHPGUSR = "SRCHLANG=en&IG=1D02BFBA1FA04166A6A7D0AF4481DAE6&BRW=XW&BRH=M&CW=1872&CH=958&SCW=1857&SCH=1456&DPR=1.0&UTC=420&DM=0&PV=15.0.0&PRVCW=1872&PRVCH=958&WTS=63856351756&CIBV=1.1788.0&EXLTT=1&HV=1720763949"

class Item(BaseModel):
    prompt: str

@app.post("/api/v1/smartchat/image_generate")
def get_images(a: Item):
    session = requests.Session()
    session.headers = HEADERS
    session.cookies.set("_U", auth_cookie)
    session.cookies.set("SRCHHPGUSR", auth_cookie_SRCHHPGUSR)

    url_encoded_prompt = requests.utils.quote(a.prompt)
    payload = f"q={url_encoded_prompt}&qs=ds"
    url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GENCRE"

    response = session.post(
        url,
        allow_redirects=False,
        data=payload,
        timeout=200,
    )

    if "this prompt is being reviewed" in response.text.lower():
        raise HTTPException(status_code=400, detail=error_being_reviewed_prompt)
    if "this prompt has been blocked" in response.text.lower():
        raise HTTPException(status_code=400, detail=error_blocked_prompt)
    if "we're working hard to offer image creator in more languages" in response.text.lower():
        raise HTTPException(status_code=400, detail=error_unsupported_lang)
    if response.status_code != 302:
        url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=3&FORM=GENCRE"
        response = session.post(url, allow_redirects=False, timeout=200)
        if response.status_code != 302:
            raise HTTPException(status_code=400, detail=error_redirect)

    redirect_url = response.headers["Location"].replace("&nfy=1", "")
    request_id = redirect_url.split("id=")[-1]
    session.get(f"{BING_URL}{redirect_url}")

    polling_url = f"{BING_URL}/images/create/async/results/{request_id}?q={url_encoded_prompt}"
    while True:
        response = session.get(polling_url)
        if response.status_code != 200:
            raise HTTPException(status_code=400, detail=error_noresults)
        if not response.text or response.text.find("errorMessage") != -1:
            continue
        else:
            break

    image_links = regex.findall(r'src="([^"]+)"', response.text)
    normal_image_links = [link.split("?w=")[0] for link in image_links]
    normal_image_links = list(set(normal_image_links))

    bad_images = [
        "https://r.bing.com/rp/in-2zU3AJUdkgFe7ZKv19yPBHVs.png",
        "https://r.bing.com/rp/TX9QuO3WzcCJz1uaaSwQAz39Kb0.jpg",
    ]
    for img in normal_image_links:
        if img in bad_images:
            raise HTTPException(status_code=400, detail=error_bad_images)

    if not normal_image_links:
        raise HTTPException(status_code=400, detail=error_no_images)
 
    return JSONResponse(content={"data": normal_image_links})