Browse Source

リンク・カードちょっと修正

kiriban
みてるぞ 2 months ago
parent
commit
c8cff9a3dd
1 changed files with 35 additions and 3 deletions
  1. +35
    -3
      main.py

+ 35
- 3
main.py View File

@@ -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:])

Loading…
Cancel
Save