gen_designer_report.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from typing import NoReturn
  2. from selenium import webdriver
  3. import time
  4. import networkx as nx
  5. import dataset
  6. import pickle
  7. import codecs
  8. import sys
  9. import os
  10. import time
  11. import re
  12. import pandas as pd
  13. import datetime
  14. import dataset
  15. import jinja2
  16. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  17. fname=os.path.abspath(__file__)
  18. elmts=fname.split(os.path.sep)
  19. path2=os.path.sep.join(elmts[0:-1])
  20. keysdir=path2+os.path.sep+'static'+os.path.sep
  21. templateLoader = jinja2.FileSystemLoader(searchpath=keysdir)
  22. templateEnv = jinja2.Environment(loader=templateLoader)
  23. TEMPLATE_FILE = "designer.tmpl"
  24. template = templateEnv.get_template(TEMPLATE_FILE)
  25. repeat={}
  26. displaydata=[]
  27. cursor=db.query('SELECT designer,searchome,c_100,hhh,dt FROM hhh.rank_summary order by dt desc,hhh asc;')
  28. for c in cursor:
  29. designer=c['designer']
  30. if repeat.get(designer) is None:
  31. repeat[designer]=1
  32. hhh=c['hhh']
  33. c_100=c['c_100']
  34. searchome=c['searchome']
  35. hhh_label='<a href="https://www.google.com/search?q='+designer+'"><button class="btn btn-primary">幸福空間 <span class="badge">'+str(c['hhh'])+'</span> </button></a>'
  36. if c_100==-1:
  37. c100_label='<span>&nbsp;</span>'
  38. else:
  39. # sch_label='<span>設計家('+str(c['searchome'])+')</span>'
  40. c100_label='<a href="https://www.google.com/search?q='+designer+'"><button class="btn btn-danger">100設計 <span class="badge">'+str(c['c_100'])+'</span> </button></a>'
  41. if searchome==-1:
  42. sch_label='<span>&nbsp;</span>'
  43. else:
  44. # sch_label='<span>設計家('+str(c['searchome'])+')</span>'
  45. sch_label='<a href="https://www.google.com/search?q='+designer+'"><button class="btn btn-danger">設計家 <span class="badge">'+str(c['searchome'])+'</span> </button></a>'
  46. local_lst=[]
  47. if hhh > 0:
  48. local_lst.append(('hhh',hhh))
  49. if searchome > 0:
  50. local_lst.append(('searchome',searchome))
  51. if c_100 > 0:
  52. local_lst.append(('c_100',c_100))
  53. sorted_by_second = sorted(local_lst, key=lambda tup: tup[1])
  54. print(sorted_by_second)
  55. # local_lst.sort()
  56. # if hhh<searchome:
  57. # first=hhh_label
  58. # if searchome >0:
  59. # if c_100>0:
  60. # if c_100 < searchome
  61. # second=sch_label
  62. # else:
  63. first=None
  64. second=None
  65. slen=len(sorted_by_second)
  66. results=[]
  67. for l in sorted_by_second:
  68. if l[0]=='hhh':
  69. results.append(hhh_label)
  70. if l[0]=='searchome':
  71. results.append(sch_label)
  72. if l[0]=='c_100':
  73. results.append(c100_label)
  74. if slen==1:
  75. results.append('<span>&nbsp;</span>')
  76. displaydata.append({'designer':designer,'first':results[0],'second':results[1],'dt':c['dt']})
  77. #
  78. #displaydata=[{'a':'aaa','b':'bbb','c':'ccc'},{'a':'aaa2','b':'bbb2','c':'ccc2'}]
  79. outputText = template.render(displaydata=displaydata)
  80. fw=codecs.open('c:/tmp/output.html','w','utf-8')
  81. fw.write(outputText)
  82. fw.close()
  83. os.startfile('c:/tmp/output.html')
  84. print(outputText)