Spaces:
Sleeping
Sleeping
import requests | |
import re | |
import json | |
import gradio as gr | |
def ReRequestUrl(url): | |
try: | |
if 'gravity' not in url: | |
return '' | |
Newurl = 'http://' + re.findall('http://(.*?)"', url)[0] | |
signed = re.findall('sign=(.*?)&', Newurl)[0] | |
FinnalUrl = Newurl.replace(signed, 'fsdfgasdgf34578348jiren') | |
req = requests.get(FinnalUrl) | |
if req.status_code == 200: | |
return req.url | |
except: | |
return '' | |
def GetList(data): | |
if len(data) == 0: | |
newJson = {} | |
newJson['type'] = 'string' | |
return newJson | |
else: | |
data = data[0] | |
if type(data) == dict: | |
newItemjson = GetJson(data) | |
elif type(data) == list: | |
newItemjson = GetList(data) | |
elif type(data) == str: | |
newItemjson = {} | |
newItemjson['type'] = 'string' | |
else: | |
newItemjson = {} | |
newItemjson['type'] = 'string' | |
return newItemjson | |
return 'error' | |
def GetJson(data): | |
newJson = {} | |
newJson['type'] = 'object' | |
newJson['required'] = [] | |
propertiesJson = {} | |
for key, value in data.items(): | |
# print(key,value) | |
if type(value) == str: | |
propertiesJson[key] = {'type': 'string'} | |
elif type(value) == int: | |
propertiesJson[key] = {'type': 'number'} | |
elif type(value) == list: | |
newListJson = {} | |
newListJson['type'] = 'array' | |
newListJson['items'] = GetList(value) | |
propertiesJson[key] = newListJson | |
elif type(value) == dict: | |
newListJson = GetJson(value) | |
propertiesJson[key] = newListJson | |
else: | |
# print(value, type(value)) | |
propertiesJson[key] = {"type":None} | |
newJson['properties'] = propertiesJson | |
return newJson | |
def gerTtr(typeStr): | |
str = str(typeStr) | |
NewStr = re.findall('<class \'(.*?)\'>', str) | |
return NewStr | |
def excuJson(html, jsonOuputPath): | |
# jsonOuputPath='jsonOutput.json' | |
try: | |
data = json.loads(html) | |
except: | |
data = json.load(html) | |
# print(data) | |
# newJson==GetJson(data) | |
listL = [] | |
dictL = {} | |
jsonOutput = GetJson(data) | |
# print(jsonOutput) | |
json.dump(jsonOutput, open(jsonOuputPath, 'w', encoding='utf-8'), indent=2, ensure_ascii=False) | |
# print(gerTtr(type('uk123'))) | |
return jsonOutput | |
def getHtml(url): | |
try: | |
headers = { | |
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36'} | |
res = requests.get(url, headers=headers) | |
if res.status_code == 200: | |
return res.text | |
else: | |
print('Error Code:' + str(res.status_code)) | |
return '' | |
except Exception as e: | |
print(e) | |
def getITP(Request): | |
# Request = 'curl -H "Host: pre-release.gravity.place" -H "User-Agent: okhttp/3.12.13" --compressed "http://pre-release.gravity.place/gravity/metaverse/starUserList?country=CN&product=gravity&star_id=23&sys_lang=ja&uwd=YRHBwL8DHv04H3JUkjFuiWFQgdmQxIy64bp7qHpU4JbPo1wW6IcEaMMp6LsT34op%0A&app_version=7.7.0&user_country=CN&sign=31caf01c033f194abc5287df89ff80bb&source=star_detail&pkg=anonymous.sns.community.gravity&token=1QZVaDtlJ21T9CYEPRAKqC1X14y6xD5%252Bfetlx%252FTJE%252Bnl2lganNSgB9BSTNgdUCCN&languageV2=ja&referrer=Organic&zone=8&system_version=10&sdk_version=29&model=V2023A&device=android&brand=vivo&ts=1686842126"' | |
try: | |
dataJson=json.loads(str(Request)) | |
jsonOuputPath = 'outputJson.json' | |
jsonOutput = excuJson(dataJson, jsonOuputPath) | |
url='' | |
except: | |
url = ReRequestUrl(Request) | |
html = getHtml(url) | |
jsonOuputPath = 'outputJson.json' | |
jsonOutput = excuJson(html, jsonOuputPath) | |
return url, showJson(jsonOutput) | |
def showJson(jsondata): | |
jsonStr = str(jsondata).replace(' ', '').replace('\n', '') | |
outPutJson = '' | |
spaceCount = 0 | |
lines = [] | |
line = '' | |
for i in range(len(jsonStr)): | |
char1 = jsonStr[i] | |
if char1 == '{': | |
spaceCount += 1 | |
outPutJson = outPutJson + char1 + '\n' | |
line += char1 + '\n' + spaceCount * 2 * ' ' | |
lines.append(line) | |
line = '' | |
elif char1 == '}' and i != len(jsonStr) - 1 and jsonStr[i + 1] != ',': | |
spaceCount -= 1 | |
outPutJson = outPutJson + '\n' + spaceCount * 2 * ' ' + char1 | |
line += '\n' + spaceCount * 2 * ' ' + char1 | |
lines.append(line) | |
line = '' | |
elif char1 == '}' and i != len(jsonStr) - 1 and jsonStr[i + 1] == ',': | |
spaceCount -= 1 | |
outPutJson = outPutJson + '\n' + jsonStr[i + 1] | |
line += '\n' + spaceCount * 2 * ' ' + char1 | |
lines.append(line) | |
line = '' | |
i += 1 | |
elif char1 == ',': | |
outPutJson = outPutJson + char1+'\n' | |
line += char1+'\n' + spaceCount * 2 * ' ' | |
lines.append(line) | |
line = '' | |
else: | |
outPutJson = outPutJson + char1 | |
line += char1 | |
#print(len(lines)) | |
#print(''.join(lines)) | |
return ''.join(lines).replace('\'','\"').replace('None','null')+'\n'+'}' | |
def main(): | |
print('欢迎来到ITP监控平台系统') | |
while True: | |
try: | |
Request = input('请输入从Charles中抓包的URLs:') | |
# Request = 'curl -H "Host: pre-release.gravity.place" -H "User-Agent: okhttp/3.12.13" --compressed "http://pre-release.gravity.place/gravity/metaverse/starUserList?country=CN&product=gravity&star_id=23&sys_lang=ja&uwd=YRHBwL8DHv04H3JUkjFuiWFQgdmQxIy64bp7qHpU4JbPo1wW6IcEaMMp6LsT34op%0A&app_version=7.7.0&user_country=CN&sign=31caf01c033f194abc5287df89ff80bb&source=star_detail&pkg=anonymous.sns.community.gravity&token=1QZVaDtlJ21T9CYEPRAKqC1X14y6xD5%252Bfetlx%252FTJE%252Bnl2lganNSgB9BSTNgdUCCN&languageV2=ja&referrer=Organic&zone=8&system_version=10&sdk_version=29&model=V2023A&device=android&brand=vivo&ts=1686842126"' | |
url = ReRequestUrl(Request) | |
print(url) | |
html = getHtml(url) | |
jsonOuputPath = 'outputJson.json' | |
with open('jsonUrl.json', 'r') as outfile: | |
html=str(json.load(outfile)) | |
print(html) | |
jsondata = excuJson(html, jsonOuputPath) | |
NewJsonStr = showJson(jsondata) | |
if NewJsonStr[0]==',': | |
NewJsonStr=NewJsonStr[1:] | |
print(NewJsonStr) | |
except Exception as e: | |
print(e) | |
def GradioMain(): | |
textbook = gr.Text(lines=2, placeholder='Requests Url') | |
grd = gr.Interface(fn=getITP, inputs=textbook, outputs=['text', 'text'], title="ITP proudct Interface system ", | |
description="将文件用charles 抓包得到的requests/json 转化成 可以直接用的Url 和json", examples=[[ | |
'curl -H "Host: pre-release.gravity.place" -H "User-Agent: okhttp/3.12.13" --compressed "http://pre-release.gravity.place/gravity/metaverse/starUserList?country=CN&product=gravity&star_id=23&sys_lang=ja&uwd=YRHBwL8DHv04H3JUkjFuiWFQgdmQxIy64bp7qHpU4JbPo1wW6IcEaMMp6LsT34op%0A&app_version=7.7.0&user_country=CN&sign=31caf01c033f194abc5287df89ff80bb&source=star_detail&pkg=anonymous.sns.community.gravity&token=1QZVaDtlJ21T9CYEPRAKqC1X14y6xD5%252Bfetlx%252FTJE%252Bnl2lganNSgB9BSTNgdUCCN&languageV2=ja&referrer=Organic&zone=8&system_version=10&sdk_version=29&model=V2023A&device=android&brand=vivo&ts=1686842126"']]) | |
grd.launch() | |
if "__main__" == __name__: | |
# main() | |
GradioMain() | |