Browse Source

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

main
みてるぞ 2 weeks 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 import sys


from atproto import Client, models from atproto import Client, models
from bs4 import BeautifulSoup
import requests import requests


from ai.talk import Talk from ai.talk import Talk
@@ -91,11 +92,14 @@ def main (
if e['contentId'] not in watched_videos]: if e['contentId'] not in watched_videos]:
watched_videos += [datum['contentId']] 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 ( embed_external = models.AppBskyEmbedExternal.Main (
external = models.AppBskyEmbedExternal.External ( 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""" client.post (Talk.main (f"""
ニコニコに『{ datum['title'] }』という動画がアップされました。 ニコニコに『{ datum['title'] }』という動画がアップされました。
つけられたタグは「{ '」、「'.join (datum['tags']) }」です。 つけられたタグは「{ '」、「'.join (datum['tags']) }」です。
@@ -175,5 +179,33 @@ def get_nico_deerjika ():
return data 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__': if __name__ == '__main__':
main (*sys.argv[1:]) main (*sys.argv[1:])

Loading…
Cancel
Save