RedPlug's Tory

latest 변수와 latest.txt파일에 제목을 비교해서 동일한 값이 아닐 경우 새글이 올라왔어요 + 글제목 + 링크를 함께 텔레그렘 봇이 보내주는 코드

텔레그램 봇 사진 발급 필요.

requests, bs4, telegram봇 모듈 설치 필요.

새글이 있을 경우 텔레그램 봇이 뿌려줌

반복은 crontab을 통해서 원하는 기간별로 진행

#tigpnd.py
# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
import os
import time

import telegram


bot = telegram.Bot(token='텔레그램봇토큰')
chat_id = bot.getUpdates()[-1].message.chat.id

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
req = requests.get('https://www.thisisgame.com/pad/tboard/?board=25')
html = req.text
soup = BeautifulSoup(html, 'html.parser')
posts = soup.select('td > a')
latest = posts[14].text
href = posts[14].get('href')
url = 'https://www.thisisgame.com/pad/tboard/'
link = url + str(href)
with open(os.path.join(BASE_DIR, 'latest.txt'), 'r+') as f_read:
    before = f_read.readline()
    if before != latest:
        bot.sendMessage(chat_id=chat_id, text='새 글이 올라왔어요! : ' + latest + link)
    f_read.close()

with open(os.path.join(BASE_DIR, 'latest.txt'), 'w+') as f_write:
    f_write.write(latest)
    print(latest)
    f_write.close()