implement, working for taito-ku rss

This commit is contained in:
Kaz Saita(raspi5) 2024-05-05 22:05:36 +09:00
parent db34654ace
commit ef03ffaf00
3 changed files with 126 additions and 12 deletions

2
.gitignore vendored
View file

@ -1,3 +1,5 @@
.venv/* .venv/*
poetry.lock poetry.lock
__pycache__/ __pycache__/
.env
config.yaml

View file

@ -1,8 +1,8 @@
[tool.poetry] [tool.poetry]
name = "python-project-template" name = "rss-mastodon-poster"
version = "0.1.0" version = "0.1.0"
description = "template project for pyenv+poetry" description = "rss -> mastodon"
authors = ["Kaz Saita(raspi4) <saita@kinoshita-lab.org>"] authors = ["Kaz Saita <saita@kinoshita-lab.org>"]
license = "MIT" license = "MIT"
readme = "README.md" readme = "README.md"
package-mode = false package-mode = false
@ -10,6 +10,10 @@ package-mode = false
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.11" python = "^3.11"
feedparser = "^6.0.11" feedparser = "^6.0.11"
mastodon-py = "^1.8.1"
python-dotenv = "^1.0.1"
pyyaml = "^6.0.1"
python-dateutil = "^2.9.0.post0"
[build-system] [build-system]

View file

@ -1,22 +1,130 @@
# !/usr/bin/env python3 # !/usr/bin/env python3
import dotenv
dotenv.load_dotenv()
import os
import feedparser import feedparser
from mastodon import Mastodon
import pprint import pprint
import yaml
import datetime
from dateutil import parser as datetimeparser
import zoneinfo
import time
# rss_url = "https://www.city.taito.lg.jp/rss_news.xml" config_file = 'config.yaml'
rss_url = 'https://notes.kinoshita-lab.org/index.xml' items = []
last_toot_id = ''
rss_url = ''
mastodon = None
config_parsed = {}
class RssItem:
def __init__(self, title :str, date :str , url :str, id :str) -> None:
self.title = title
self.date = date
self.url = url
self.id = id
def main():
def readRss() -> None:
global config_file
global config_parsed
global rss_url
last_id = ''
def read_config() -> None:
global rss_url
global config_parsed
nonlocal last_id
try:
with open(config_file, 'r') as file:
config_parsed = yaml.safe_load(file) or {}
rss_url = config_parsed.get('RSS_URL')
last_id = config_parsed.get('LAST_ID')
except Exception as e:
print("Read config error: ", e)
read_config()
parsed = feedparser.parse(rss_url) parsed = feedparser.parse(rss_url)
for entry in parsed.entries: for entry in parsed.entries:
print("id: {}".format(entry.id))
print("title: {}".format(entry.title)) if entry.id == last_id:
print("link: {}".format(entry.link)) print(f'found last processed id: {last_id}, stop')
print("published: {}".format(entry.published)) break
# print("description: {}".format(entry.description))
# pprint.pprint(entry)
date = datetimeparser.parse(entry.published)
date_timezone = date.astimezone(zoneinfo.ZoneInfo('Japan'))
formatted_date = date_timezone.strftime('%Y/%m/%d %H:%M')
print(formatted_date)
item = RssItem(entry.title, formatted_date, entry.link, entry.id)
items.append(item)
items.reverse() # old to new order
def login() -> Mastodon:
url = os.getenv('MASTODON_URL')
if (url == None):
print("error: no mastodon url specified.")
exit()
token = os.getenv('MASTODON_TOKEN')
if (token == None):
print("error: no mastodon token specified.")
exit()
m = Mastodon(
access_token=token,
api_base_url=url
)
return m
def post_to_mastodon(message: str) -> bool:
global mastodon
print(message)
if mastodon == None:
return False
mastodon.status_post(message)
return True
def main() -> None:
global mastodon
global items
global config_parsed
readRss()
mastodon = login()
for item in items:
message = item.title + '\n'
message += item.date + '\n\n'
message += item.url + ''
result = post_to_mastodon(message)
if result == True:
config_parsed['LAST_ID'] = item.id
with open(config_file, 'w') as f:
yaml.dump(config_parsed, f, default_flow_style=False)
time.sleep(10)
if __name__ == '__main__': if __name__ == '__main__':
main() main()