From c8cff9a3dd088cec58b4df16503e10e7198ddb25 Mon Sep 17 00:00:00 2001 From: miteruzo Date: Sat, 7 Sep 2024 06:51:57 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=AF=E3=83=BB=E3=82=AB?= =?UTF-8?q?=E3=83=BC=E3=83=89=E3=81=A1=E3=82=87=E3=81=A3=E3=81=A8=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index d45a2af..04566e5 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import time import sys from atproto import Client, models +from bs4 import BeautifulSoup import requests from ai.talk import Talk @@ -91,11 +92,14 @@ def main ( if e['contentId'] not in watched_videos]: watched_videos += [datum['contentId']] + uri = f"https://www.nicovideo.jp/watch/{ datum['contentId'] }" + (title, description, thumb) = get_embed_info (uri) embed_external = models.AppBskyEmbedExternal.Main ( external = models.AppBskyEmbedExternal.External ( - title = datum['title'], - description = datum['description'], - uri = f"https://www.nicovideo.jp/watch/{ datum['contentId'] }")) + title = title, + description = description, + thumb = thumb, + uri = uri)) client.post (Talk.main (f""" ニコニコに『{ datum['title'] }』という動画がアップされました。 つけられたタグは「{ '」、「'.join (datum['tags']) }」です。 @@ -175,5 +179,33 @@ def get_nico_deerjika (): return data +def get_embed_info ( + url: str +) -> (str, str, str): + try: + res = requests.get (url, timeout = 60) + except Exception: + return ('', '', '') + + if res.status_code != 200: + return ('', '', '') + + soup = BeautifulSoup (res.text, 'html.parser') + + tmp = soup.find ('title') + if tmp is not None: + title = tmp.text + + tmp = soup.find ('meta', attrs = { 'name': 'description' }) + if tmp is not None: + description = tmp.get ('content') + + tmp = soup.find ('meta', attrs = { 'name': 'thumbnail' }) + if tmp is not None: + thumbnail = tmp.get (' content') + + return (title, description, thumbnail) + + if __name__ == '__main__': main (*sys.argv[1:])