RSSフィードをLINEで読む.py
LINE Notify
API KEYを取得して環境変数に格納しておく
projectnameに自分のScrapboxプロジェクト名を入れる
自分のプロジェクトにwatchlistというページを作り、フィードのURLを改行区切りで書いておく
code:notify.py
import requests
import os
import feedparser
from bs4 import BeautifulSoup
import time
os.system("touch .urls")
with open(".urls", 'r') as f:
urls = f.readlines()
projectname = "ここにプロジェクト名を入力"
def send_line_notify(notification_message):
"""
LINEに通知する
"""
line_notify_token = os.environ.get('LINE_API_KEY')
line_notify_api = 'https://notify-api.line.me/api/notify'
headers = {'Authorization': f'Bearer {line_notify_token}'}
data = {'message': f' {notification_message}'}
requests.post(line_notify_api, headers=headers, data=data)
feeds = requests.get(
'https://scrapbox.io/api/pages/{}/watchlist/text'.format(projectname)).text.split("\n")[1:-1]
def fetchpush():
for _ in feeds:
d = feedparser.parse(_)
for e in d.entries:
if e.links0.href+'\n' not in urls:
try:
send_line_notify(
e.title+"\n\n"+BeautifulSoup(e.summary, 'lxml').text)
print(e.title+"\n\n"+BeautifulSoup(e.summary, 'lxml').text)
with open(".urls", 'a') as f:
f.write(e.links0.href+"\n")
urls.append(e.links0.href)
except AttributeError:
pass
while 1:
fetchpush()
time.sleep(200)
def send_line_notify(notification_message):
line_notify_token = os.environ.get('LINE_API_KEY')
line_notify_api = 'https://notify-api.line.me/api/notify'
headers = {'Authorization': f'Bearer {line_notify_token}'}
data = {'message': f' {notification_message}'}
requests.post(line_notify_api, headers=headers, data=data)