#!/usr/bin/python3 from pytrends.request import TrendReq import pytrends import sys import datetime import traceback import requests from bs4 import BeautifulSoup import re #import farmlib import pandas as pd import random import os import dataset import json from gn3 import GoogleNews3 googlenews = GoogleNews3(lang="zh-tw", period="7d", encode="utf-8", region="tw") # googlenews.search('蔡英文') """ result = googlenews.gettext() for n in range(len(result)): print(n) print(result[n]) result = googlenews.get_links() for n in range(len(result)): print(n) print(result[n]) """ """ googlenews.get_news('蔡英文') trdresult = googlenews.result() #print(trdresult[0]['title']) for article in trdresult: print(article['title']) """ db = dataset.connect( 'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4') cursor = db.query( '(SELECT * FROM trending_searches ORDER BY ts_date DESC LIMIT 20) ORDER BY ts_date') """ for c in cursor: print(c['ts_word']) """ # print(str(datetime.date.today()).replace('-','')) trpath = os.getcwd() + '/../webSite/content/trends/' trdpath = os.getcwd() + '/../webSite/content/trenddetail/' with open(trpath + str(datetime.date.today()).replace('-', '')+'.md', 'w', encoding='UTF-8') as f: f.write('+++\n') f.write('title = "趨勢日報"\n' + 'date = "' + str(datetime.datetime.now()) + '"\n' + 'tags = ["daily-trend-watch"]\n' + 'categories = ["trends"]\n' + 'layout = "trends"\n' + 'banner = "img/banners/banner-3.jpg"\n') for c in cursor: f.write('no' + str(c['ts_rank']).zfill(2) + ' = "' + c['ts_word'] + '"\n') googlenews.get_news(c['ts_word']) trdresult = googlenews.result() with open(trdpath + str(datetime.date.today()).replace('-', '')+'_' + str(c['ts_rank']).zfill(2)+'.md', 'w', encoding='UTF-8') as fd: fd.write('+++\n') fd.write('title = "趨勢日報"\n' + 'date = "' + str(datetime.datetime.now()) + '"\n' + 'layout = "trends"\n' + 'banner = "img/banners/banner-3.jpg"\n') rcount = 1 print(c['ts_word']) for article in trdresult: fd.write('title' + str(rcount)+' = "' + str(article['title']) + '"\n' + 'desc' + str(rcount)+' = "' + str(article['desc']) + '"\n' + 'date' + str(rcount)+' = "' + str(article['date']) + '"\n' + 'datetime' + str(rcount)+' = "' + str(article['datetime']) + '"\n' + 'link' + str(rcount)+' = "' + str(article['link']) + '"\n' + 'img' + str(rcount)+' = "' + str(article['img']) + '"\n' + 'media' + str(rcount)+' = "' + str(article['media']) + '"\n' + 'site' + str(rcount)+' = "' + str(article['site']) + '"\n') # print(article) rcount += 1 fd.write('+++\n') f.write('+++\n')