123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- from pydoc import HTMLDoc
- from fastapi import FastAPI
- import dataset
- import sys
- import os
- import time
- from fastapi.middleware.cors import CORSMiddleware
- from fastapi.staticfiles import StaticFiles
- from pydantic import BaseModel
- from fastapi import FastAPI, Form
- import subprocess
- import suggests
- from typing import Optional
- import networkx as nx
- import pyvis
- import time
- from pyvis.network import Network
- import pickle
- import logging
- import threading
- import time
- import random
- import string
- from fastapi.responses import HTMLResponse
- from fastapi.responses import RedirectResponse
- def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
- return ''.join(random.choice(chars) for _ in range(size))
- app = FastAPI()
- origins = ["*"]
- app.add_middleware(
- CORSMiddleware,
- allow_origins=origins,
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4')
- app.mount("/web", StaticFiles(directory="static"), name="static")
- def thread_function(kw):
- global db
- print(kw)
- G = nx.Graph()
- for k in kw:
- s = suggests.suggests.get_suggests(k, source='google')
- for sg in s['suggests']:
- G.add_edge(k,sg,weight=1)
- print(sg)
- time.sleep(1)
- s2 = suggests.suggests.get_suggests(k, source='google')
- for elmt in s2['suggests']:
- G.add_edge(sg,elmt,weight=1)
- G.remove_nodes_from(list(nx.isolates(G)))
- G.remove_edges_from( list(nx.selfloop_edges(G)))
- # pickle.dump( G, open( "gs2.p", "wb" ) )
- pyG = Network(height="750px", width="100%",bgcolor="#333333",font_color="white")
- pyG.from_nx(G)
- id=id_generator()
- db['gen_graph'].insert({'filename':str(id),'kw':str(kw)})
- # pyG.save_graph('gstest')
- # pyG.show('static/gs/'+str(id)+'.html')
- pyG.save_graph('static/gs/'+str(id)+'.html')
- @app.get("/tree_list/",response_class=HTMLResponse)
- async def tree_list():
- # global db
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/gtrends?charset=utf8mb4')
- html="<html><body><h2>清單</h2></br>請一分鐘後refresh </br></br>"
- html+="<table border='1'>"
- cursor=db.query('select filename,kw from gen_graph order by id desc')
- cnt=0
- for c in cursor:
- html+="<tr><td>"+c['kw']+"</td>"
- html+="<td><a href='/web/gs/"+c['filename']+".html'>"+c['filename']+"</a></td></tr>"
- cnt+=1
- if cnt > 10:
- break
-
- html+="</table></body></html>"
- return html
- #response_class=RedirectResponse
- @app.post("/gen_tree/",response_class=HTMLResponse)
- async def func_expand(kw: str = Form(...),kw2:Optional[str] = Form(None) ):
- kwlst=[]
- if len(kw)>1:
- kwlst.append(kw)
- if kw2 is not None:
- kwlst.append(kw2)
- x = threading.Thread(target=thread_function, args=(kwlst,))
- x.start()
- # return "ok"
- return RedirectResponse(url="/tree_list",status_code=302)
- # return HTMLResponse('<html><head><meta http-equiv="refresh" content="0; URL="/tree_list" /></head></html>')
|