DB_gap.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/python3
  2. import sys
  3. import codecs
  4. import traceback
  5. import requests
  6. import re
  7. import pandas as pd
  8. import random
  9. import urllib
  10. import dataset
  11. import json
  12. import gspread
  13. import datetime
  14. from gspread_pandas import Spread, Client
  15. from oauth2client.service_account import ServiceAccountCredentials
  16. import os
  17. import threading
  18. def save_sheet(df,filename,tabname,startpos='A1'):
  19. scope = ['https://spreadsheets.google.com/feeds',
  20. 'https://www.googleapis.com/auth/drive']
  21. credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
  22. # credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)
  23. gc = gspread.authorize(credentials)
  24. spread = Spread(filename,creds=credentials)
  25. spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
  26. def do_jobs():
  27. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
  28. # cursor=db.query("SELECT q,cnt FROM hhh.gap_searchome order by cnt desc")
  29. # cursor=db.query("SELECT q,cnt FROM hhh.gap_hhh order by cnt desc")
  30. # cursor=db.query("SELECT q,cnt FROM hhh.gap_searchome where q not in (select q from hhh.gap_hhh) order by cnt desc;")
  31. cursor=db.query("SELECT q,cnt FROM hhh.gap_d100 where q not in (select q from hhh.gap_hhh) order by cnt desc;")
  32. df = pd.DataFrame(columns=('q','cnt'))
  33. idx=0
  34. for c in cursor:
  35. df.loc[idx]=[c['q'],c['cnt']]
  36. idx+=1
  37. # save_sheet(df,'GapAnalysis','searchome')
  38. # save_sheet(df,'GapAnalysis','hhh')
  39. # save_sheet(df,'GapAnalysis','GAP')
  40. save_sheet(df,'GapAnalysis','GAP_100')
  41. t = threading.Thread(target = do_jobs)
  42. t.start()
  43. t.join()