gen_daily_report.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/python3
  2. from pytrends.request import TrendReq
  3. import pytrends
  4. import sys
  5. import datetime
  6. import traceback
  7. import requests
  8. from bs4 import BeautifulSoup
  9. import re
  10. #import farmlib
  11. import pandas as pd
  12. import random
  13. import os
  14. import dataset
  15. import json
  16. from gn3 import GoogleNews3
  17. googlenews = GoogleNews3(lang="zh-tw", period="7d",
  18. encode="utf-8", region="tw")
  19. # googlenews.search('蔡英文')
  20. """ result = googlenews.gettext()
  21. for n in range(len(result)):
  22. print(n)
  23. print(result[n])
  24. result = googlenews.get_links()
  25. for n in range(len(result)):
  26. print(n)
  27. print(result[n]) """
  28. """ googlenews.get_news('蔡英文')
  29. trdresult = googlenews.result()
  30. #print(trdresult[0]['title'])
  31. for article in trdresult:
  32. print(article['title']) """
  33. db = dataset.connect(
  34. 'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
  35. cursor = db.query(
  36. '(SELECT * FROM trending_searches ORDER BY ts_date DESC LIMIT 20) ORDER BY ts_date')
  37. """ for c in cursor:
  38. print(c['ts_word']) """
  39. # print(str(datetime.date.today()).replace('-',''))
  40. trpath = os.getcwd() + '/../webSite/content/trends/'
  41. trdpath = os.getcwd() + '/../webSite/content/trenddetail/'
  42. with open(trpath + str(datetime.date.today()).replace('-', '')+'.md', 'w', encoding='UTF-8') as f:
  43. f.write('+++\n')
  44. f.write('title = "趨勢日報"\n' +
  45. 'date = "' + str(datetime.datetime.now()) + '"\n' +
  46. 'tags = ["daily-trend-watch"]\n' +
  47. 'categories = ["trends"]\n' +
  48. 'layout = "trends"\n' +
  49. 'banner = "img/banners/banner-3.jpg"\n')
  50. for c in cursor:
  51. f.write('no' + str(c['ts_rank']).zfill(2) +
  52. ' = "' + c['ts_word'] + '"\n')
  53. googlenews.get_news(c['ts_word'])
  54. trdresult = googlenews.result()
  55. with open(trdpath + str(datetime.date.today()).replace('-', '')+'_' + str(c['ts_rank']).zfill(2)+'.md', 'w', encoding='UTF-8') as fd:
  56. fd.write('+++\n')
  57. fd.write('title = "趨勢日報"\n' +
  58. 'date = "' + str(datetime.datetime.now()) + '"\n' +
  59. 'layout = "trends"\n' +
  60. 'banner = "img/banners/banner-3.jpg"\n')
  61. rcount = 1
  62. print(c['ts_word'])
  63. for article in trdresult:
  64. fd.write('title' + str(rcount)+' = "' + str(article['title']) + '"\n' +
  65. 'desc' + str(rcount)+' = "' + str(article['desc']) + '"\n' +
  66. 'date' + str(rcount)+' = "' + str(article['date']) + '"\n' +
  67. 'datetime' + str(rcount)+' = "' + str(article['datetime']) + '"\n' +
  68. 'link' + str(rcount)+' = "' + str(article['link']) + '"\n' +
  69. 'img' + str(rcount)+' = "' + str(article['img']) + '"\n' +
  70. 'media' + str(rcount)+' = "' + str(article['media']) + '"\n' +
  71. 'site' + str(rcount)+' = "' + str(article['site']) + '"\n')
  72. # print(article)
  73. rcount += 1
  74. fd.write('+++\n')
  75. f.write('+++\n')