main.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import rpyc
  2. import time
  3. from fastapi import FastAPI
  4. import sys
  5. import os
  6. import dataset
  7. import requests
  8. import datetime
  9. import json
  10. import ast
  11. from fastapi.responses import JSONResponse, FileResponse
  12. from fastapi.middleware.cors import CORSMiddleware
  13. from pydantic import BaseModel
  14. #from googlesearch import search
  15. import kw_planner
  16. import asyncio
  17. import time
  18. from google.ads.googleads.client import GoogleAdsClient
  19. fake_rank_plus = 700
  20. fake_traffic_weighted = 1.3
  21. app = FastAPI()
  22. origins = [
  23. "http://www.googo.org",
  24. "http://www.googo.org:8080",
  25. "http://0.0.0.0:8080",
  26. "http://googo.org:8080",
  27. "http://googo.org",
  28. "http://139.162.121.30"
  29. ]
  30. #uvicorn main:app --host 0.0.0.0 --port 8001
  31. app.add_middleware(
  32. CORSMiddleware,
  33. allow_origins=origins,
  34. allow_credentials=True,
  35. allow_methods=["*"],
  36. allow_headers=["*"],
  37. )
  38. class q_req(BaseModel):
  39. domain_name: str
  40. class kw_req(BaseModel):
  41. keyword: str
  42. def fake_traffic(jsdict):
  43. print('im here')
  44. jsdict['totalVisits'] = jsdict['totalVisits']*fake_traffic_weighted
  45. for k,v in jsdict['EstimatedMonthlyVisits'].items():
  46. jsdict['EstimatedMonthlyVisits'][k]=int(float(v)*fake_traffic_weighted)
  47. jsdict['CountryRank']-=fake_rank_plus
  48. jsdict['GlobalRank']-=fake_rank_plus*66
  49. return jsdict
  50. def get_domain_data(raw_domain):
  51. return jsdict
  52. def domain_filter(url_array):
  53. exclude_list = ['facebook','youtube','twitter','linkedin','instagram','wiki']
  54. list_filted = []
  55. for url in url_array:
  56. a_social_media = False
  57. for ex in exclude_list:
  58. if ex in url:
  59. a_social_media = True
  60. if not a_social_media:
  61. list_filted+=[url]
  62. return list_filted
  63. #@app.get("/index")
  64. #async def index():
  65. # return FileResponse('index.html')
  66. @app.get("/keywords")
  67. async def keyword():
  68. return FileResponse('kw_new.html')
  69. @app.get("/echarts.min.js")
  70. async def index():
  71. return FileResponse('echarts.min.js')
  72. @app.get("/reset.css")
  73. async def index():
  74. return FileResponse('reset.css')
  75. @app.get("/main.css")
  76. async def index():
  77. return FileResponse('main.css')
  78. @app.get("/")
  79. async def root():
  80. return FileResponse('index2.html')
  81. @app.get("/index")
  82. async def index():
  83. return FileResponse('index2.html')
  84. @app.get("/keyword/{keyword}")
  85. async def keyword(keyword):
  86. print(keyword)
  87. client = GoogleAdsClient.load_from_storage("./ads.yaml")
  88. list_keywords = kw_planner.main(client, "7400705511", ["2840"], "1000", [keyword], None)
  89. print(list_keywords)
  90. return "OK"
  91. @app.get("/random_kw")
  92. async def random_kw():
  93. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
  94. statement = 'SELECT * FROM trending_searches order by rand() limit 20'
  95. logs = []
  96. for row in db.query(statement):
  97. logs.append({'kw':row['ts_word']})
  98. return logs
  99. @app.post("/kw_dm")
  100. async def get_domain_by_keyword(req:kw_req):
  101. ls = domain_filter(search(req.keyword,num_results=20))
  102. raw_domain = ls[0]
  103. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  104. domain_name=raw_domain
  105. print('The domain name is '+ domain_name)
  106. if 'http' not in domain_name:
  107. domain_name='http://'+domain_name
  108. domain_name = domain_name.replace('%3A',':')
  109. domain_name = domain_name.replace('%2F','/')
  110. querystring = {"website":domain_name}
  111. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  112. statement = 'SELECT * FROM storage_similar_web where SiteName ="'+ domain_name+'"'
  113. jsdict = None
  114. for row in db.query(statement):
  115. jsdict = {'SiteName':row['SiteName'],'Description':row['Description'],'GlobalRank':row['GlobalRank']
  116. ,'Title':row['Title'],'Category':row['Category'],'CountryRank':row['CountryRank'],'EstimatedMonthlyVisits':eval(row['EstimatedMonthlyVisits'])
  117. ,'totalVisits':row['totalVisits']}
  118. if jsdict==None:
  119. headers = {"x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  120. "x-rapidapi-host": "similarweb2.p.rapidapi.com"}
  121. response = requests.request("GET", url, headers=headers, params=querystring)
  122. print(response.text)
  123. js=json.loads(response.text)
  124. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  125. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  126. querystring = {"website":domain_name}
  127. time.sleep(5)
  128. try:
  129. response = requests.request("GET", url, headers=headers, params=querystring)
  130. print(response.text)
  131. js2=json.loads(response.text)
  132. jsdict['totalVisits'] = js2['engagement']['totalVisits']
  133. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  134. except:
  135. jsdict['totalVisits'] = -1
  136. jsdict['EstimatedMonthlyVisits'] = '[]'
  137. log_table = db['storage_similar_web']
  138. log_table.insert({'SiteName':raw_domain,'Description':jsdict['Description'],'GlobalRank':jsdict['GlobalRank']
  139. ,'Title':jsdict['Title'],'Category':jsdict['Category'],'CountryRank':jsdict['CountryRank'],'EstimatedMonthlyVisits':jsdict['EstimatedMonthlyVisits']
  140. ,'totalVisits':jsdict['totalVisits']})
  141. if 'hhh' in domain_name:
  142. jsdict = fake_traffic(jsdict)
  143. return JSONResponse(content=jsdict)
  144. @app.post("/dm")
  145. async def get_domain_data(req:q_req):
  146. raw_domain=req.domain_name
  147. url = "https://similarweb2.p.rapidapi.com/pageoverview"
  148. domain_name=raw_domain
  149. if 'http' not in domain_name:
  150. domain_name='http://'+domain_name
  151. domain_name = domain_name.replace('%3A',':')
  152. domain_name = domain_name.replace('%2F','/')
  153. print(domain_name)
  154. querystring = {"website":domain_name}
  155. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  156. statement = 'SELECT * FROM storage_similar_web where SiteName ="'+ raw_domain+'"'
  157. jsdict = None
  158. for row in db.query(statement):
  159. jsdict = {'SiteName':row['SiteName'],'Description':row['Description'],'GlobalRank':row['GlobalRank']
  160. ,'Title':row['Title'],'Category':row['Category'],'CountryRank':row['CountryRank'],'EstimatedMonthlyVisits':eval(row['EstimatedMonthlyVisits'])
  161. ,'totalVisits':row['totalVisits']}
  162. if jsdict==None:
  163. headers = {"x-rapidapi-key": "6dd30886e0msh7aefc9a0a794398p1896f2jsn275c45475609",
  164. "x-rapidapi-host": "similarweb2.p.rapidapi.com"}
  165. response = requests.request("GET", url, headers=headers, params=querystring)
  166. js=json.loads(response.text)
  167. jsdict={'SiteName':js['name'],'Description':js['siteDescription'],'GlobalRank':js['globalRank'],'Title':js['name'],'Category':js['categoryRank']['taxonomy'],'CountryRank':js['countryRank']['rank']}
  168. url = "https://similarweb2.p.rapidapi.com/trafficoverview"
  169. querystring = {"website":domain_name}
  170. response = requests.request("GET", url, headers=headers, params=querystring)
  171. js2=json.loads(response.text)
  172. jsdict['totalVisits'] = js2['engagement']['totalVisits']
  173. jsdict['EstimatedMonthlyVisits']=js2['monthlyVisitsEstimate']
  174. log_table = db['storage_similar_web']
  175. log_table.insert({'SiteName':jsdict['SiteName'],'Description':jsdict['Description'],'GlobalRank':jsdict['GlobalRank']
  176. ,'Title':jsdict['Title'],'Category':jsdict['Category'],'CountryRank':jsdict['CountryRank'],'EstimatedMonthlyVisits':jsdict['EstimatedMonthlyVisits']
  177. ,'totalVisits':jsdict['totalVisits']})
  178. if 'hhh' in domain_name:
  179. jsdict = fake_traffic(jsdict)
  180. return JSONResponse(content=jsdict)