9
9
- `BLOG_POST_SLUG_TITLE` :
10
10
- `BLOG_POST_AUTHOR` :
11
11
"""
12
- import requests
12
+
13
+ from dataclasses import dataclass
14
+ from typing import Optional
15
+ import httpx
13
16
import bs4
14
17
import time
15
18
import os
16
19
17
20
ROOT : str = "https://blahcat.github.io"
18
- URL : str = f"{ ROOT } /feeds/all.atom.xml"
21
+ ATOM_FEED_URL : str = f"{ ROOT } /feeds/all.atom.xml"
22
+
23
+
24
+ @dataclass
25
+ class SocialMedia :
26
+ twitter : Optional [str ]
27
+ mastodon : Optional [str ]
28
+ discord : Optional [str ]
29
+ github : Optional [str ]
30
+
19
31
20
- time .sleep (10 )
32
+ AUTHORS = {
33
+ "hugsy" :
SocialMedia (
"@_hugsy_" ,
"@[email protected] " ,
"@crazy.hugsy" ,
"hugsy" )
34
+ }
21
35
22
- h = requests .get (URL )
36
+ time .sleep (2 )
37
+
38
+ h = httpx .get (ATOM_FEED_URL )
23
39
assert h .status_code == 200
24
40
25
- soup = bs4 .BeautifulSoup (h .text , "lxml " )
41
+ soup = bs4 .BeautifulSoup (h .text , "xml " )
26
42
node = soup .find ("entry" )
27
43
assert node is not None
28
44
@@ -34,33 +50,29 @@ def get(x: str):
34
50
35
51
36
52
def strip_html (html : str ):
37
- s = bs4 .BeautifulSoup (html , features = "html.parser " )
53
+ s = bs4 .BeautifulSoup (html , features = "xml " )
38
54
return s .get_text ()
39
55
40
56
57
+ def env (x : str ):
58
+ os .system (f"echo { x } >> $GITHUB_ENV" )
59
+
60
+
41
61
title = get ("title" ).text
42
62
authors = [x .text for x in get ("author" ).find_all ("name" )]
43
63
published = get ("published" ).text
44
- url = ROOT + get ("link" )["href" ]
45
- slug = get ("link" )["href" ][ 18 : - 5 ]
64
+ url = str ( get ("link" )["href" ])
65
+ slug = str ( get ("link" )["href" ]. rsplit ( "/" )[ - 1 ])
46
66
summary = strip_html (get ("summary" ).text )[:- 3 ] + " [...]"
47
67
48
- author_twitters = []
49
- for author in authors :
50
- if author == "hugsy" :
51
- author_twitters .append ("@_hugsy_" )
52
- # TODO automate this
53
-
68
+ author_twitters = [
69
+ AUTHORS [n ].twitter for n in authors if n in AUTHORS and AUTHORS [n ].twitter
70
+ ]
54
71
twitter_body = (
55
72
f"""New blog post: '{ title } ' by { ' and ' .join (author_twitters )} - { url } """
56
73
)
57
74
twitter_body = twitter_body [:280 ]
58
75
59
-
60
- def env (x : str ):
61
- os .system (f"echo { x } >> $GITHUB_ENV" )
62
-
63
-
64
76
env (f"""BLOG_POST_TITLE="{ title } " """ )
65
77
env (f"""BLOG_POST_PUBLISHED_DATE="{ published } " """ )
66
78
env (f"""BLOG_POST_URL={ url } """ )
0 commit comments