gtree.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from pydoc import HTMLDoc
  2. import dataset
  3. import sys
  4. import os
  5. import time
  6. import subprocess
  7. import suggests
  8. from typing import Optional
  9. import networkx as nx
  10. import pyvis
  11. import time
  12. from pyvis.network import Network
  13. import pickle
  14. import logging
  15. import threading
  16. import time
  17. import random
  18. import string
  19. def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
  20. return ''.join(random.choice(chars) for _ in range(size))
  21. def thread_function(kw):
  22. global db
  23. print(kw)
  24. G = nx.Graph()
  25. for k in kw:
  26. s = suggests.suggests.get_suggests(k, source='google')
  27. for sg in s['suggests']:
  28. G.add_edge(k,sg,weight=1)
  29. print(sg)
  30. time.sleep(1)
  31. s2 = suggests.suggests.get_suggests(k, source='google')
  32. for elmt in s2['suggests']:
  33. G.add_edge(sg,elmt,weight=1)
  34. # G.remove_nodes_from(list(nx.isolates(G)))
  35. G.remove_edges_from( list(nx.selfloop_edges(G)))
  36. # pickle.dump( G, open( "gs2.p", "wb" ) )
  37. pyG = Network(height="750px", width="100%",bgcolor="#333333",font_color="white")
  38. pyG.from_nx(G)
  39. id=id_generator()
  40. # db['gen_graph'].insert({'filename':str(id),'kw':str(kw)})
  41. # pyG.save_graph('gstest')
  42. # pyG.show('static/gs/'+str(id)+'.html')
  43. pyG.save_graph('static/gs/'+str(id)+'.html')
  44. thread_function('娛樂城')