import requests
import json
from bs4 import BeautifulSoup


class PTTparser:

    def __init__(self):
        self.PTTaddress = 'https://www.ptt.cc/bbs/'

    def parseBoard(self, boardName):
        pass

    def parsePage(self, boardName, pageNum):
        pass

    def parseArticle(self, boardName, articleID):
        articleURL = self.PTTaddress + boardName + '/' + articleID + '.html'
        print(articleURL)

        response = requests.get(articleURL)
        html = response.content.decode('utf-8')
        soup = BeautifulSoup(html, 'html.parser')

        metas = self.getArticleMeta(soup)
        allPushs = self.getAllPushs(soup)

        articleJSON = {
            'authorID': self.getArticleAuthorID(metas),
            'boardName': boardName,
            'title': self.getArticleTitle(metas),
            'postTime': self.getArticlePostTime(metas),
            'pushMessages': self.getArticlePushMessages(allPushs)
        }

        print('=====JSON=====')
        jsonText = json.dumps(articleJSON, sort_keys=True,
                              indent=4, ensure_ascii=False)
        print(jsonText)

    def getArticleMeta(self, soup):
        return soup.find_all('span', class_='article-meta-value')
        # return soup.find_all('span', {'class': 'article-meta-value'})

    def getAllPushs(self, soup):
        return soup.find_all('div', class_='push')

    def getArticleAuthorID(self, metas):
        return metas[0].text.split(' ')[0]

    def getArticleTitle(self, metas):
        return metas[2].text

    def getArticlePostTime(self, metas):
        return metas[3].text

    def getArticlePushMessages(self, allPushs):
        pushMessages = []
        for p in allPushs:
            message = {
                'pushTag': p.find('span', class_='push-tag').text[0],
                'pushUserID': p.find('span', class_='push-userid').text,
                'pushContent': p.find('span', class_='push-content').text[2:]
            }

            pushMessages.append(message)

        return pushMessages

@@ -246,34 +246,756 @@
 Num):%0A        pa
-ss
+geIndex = '/index' + str(pageNum) + '.html'%0A        pageURL = self.PTTaddress + boardName + pageIndex%0A        print(pageURL)%0A        print('=====')%0A%0A        response = requests.get(pageURL)%0A        html = response.content.decode('utf-8')%0A        soup = BeautifulSoup(html, 'html.parser')%0A%0A        titles = soup.find_all('div', class_='title')%0A%0A        for t in titles:%0A            if '(%E6%9C%AC%E6%96%87%E5%B7%B2%E8%A2%AB%E5%88%AA%E9%99%A4)' in t.text:%0A                print('deleted page')%0A            else:%0A                print(t.text.strip())%0A                articleLink = t.find('a').get('href')%0A                print(articleLink)%0A                articleID = articleLink.split('/')%5B3%5D.replace('.html', '')%0A                print(articleID)%0A%0A            print('-----')
 %0A%0A    def parseA

implement parsePage()