#coding=UTF-8

#!/usr/bin/python

import requests
from bs4 import BeautifulSoup
import os

# 全域變數
yahooStockUrl = 'https://tw.stock.yahoo.com/q/q?s='
user_agent = {'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36'}

def myCut(s):
   tbl = [
#      '加到',
      '\n',
   ]

   for t in tbl:
      n = s.find(t)
      if n >= 0:
         s = s[:n]
         return s
   return s

def getStockContent(url):
   res = requests.get(url, headers=user_agent, timeout=30)
   if res.status_code == requests.codes.ok:
      dir = os.path.dirname(__file__)  # 取得目前執行目錄
      fn = os.path.join(dir, stockId+'.csv')
      flag = os.path.isfile(fn)
      f = open(fn, 'a', encoding='Big5')  # 開啟寫入檔案格式 utf8

      soup = BeautifulSoup(res.text, 'html.parser')
      t = soup.find('table', attrs={'border' : '2'})
      if flag == False:
         th = t.find_all('th')
         f.write('日期,')
         for i in range(1, len(th)-1):
            f.write(th[i].text + ',')
         f.write('\n')
      upDate = soup.find('font', class_='tt').text[7:]
      f.write(upDate + ',')
      td = t.find_all('td')
      for i in range(1, len(td)-1):
         s = td[i].text.replace(',', '')
         if i == 5:
            s = myCut(s)
         f.write(s + ',')
      f.write('\n')
      f.close()

# 主程式 main
"""
stockList = [
   '2317',
   '2330',
   '2498',
   ]
for stockId in stockList:
   getStockContent(yahooStockUrl + stockId)
"""

dir = os.path.dirname(__file__)  # 取得目前執行目錄
fn = os.path.join(dir, 'stock.txt')
f = open(fn)
lines = f.readlines()
for line in lines:  # 讀取整個檔案
#   print(line)
   stockId = line.replace('\n', '')
   getStockContent(yahooStockUrl + stockId)

f.close()

input('Press enter to continue......')
