-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.py
More file actions
112 lines (93 loc) · 3.31 KB
/
Copy pathaction.py
File metadata and controls
112 lines (93 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/python3
import urllib.request
import argparse
from shlex import quote as q
import json
import csv
import os
import re
def print_and_run(a):
print(a, flush=True)
os.system(a)
post_regex = re.compile(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}-.*\.md$")
if __name__ == "__main__":
pars = argparse.ArgumentParser()
pars.add_argument("-name", dest="name", required=True)
pars.add_argument("-username", dest="username", required=True)
pars.add_argument("-gist", dest="gist", required=True)
pars.add_argument("-email", dest="email", required=True)
pars.add_argument("-branch1", dest="branch1", required=True)
pars.add_argument("-branch2", dest="branch2", required=True)
pars.add_argument("-instance", dest="instance", required=True)
pars.add_argument("-blogbase", dest="blogbase", required=True)
args = pars.parse_args()
key = os.environ.get("MASTODON_TOKEN")
gey = os.environ.get("GIST_TOKEN")
if not key:
exit(
"MASTODON_TOKEN not found. Check your secrets and environments config for your repository"
)
if not gey:
exit(
"GIST_TOKEN not found. Check your secrets and environments config for your repository"
)
print(os.getcwd(), flush=True)
print_and_run(f"git config --global user.name {q(args.name)}")
print_and_run(f"git config --global user.email {q(args.email)}")
print_and_run(f"git switch -f -C {q(args.branch2)}")
post_file = "testposts.csv"
posts = {}
try:
with open(post_file) as f:
for a in csv.reader(f, dialect="unix"):
if not a:
continue
posts[a[1]] = a[0]
except:
pass
print_and_run(f"git switch -f -C {q(args.branch1)}")
found = None
for a in os.listdir("_posts"):
if not a:
continue
if not post_regex.match(a):
continue
a = a.rsplit(".", 1)[0]
if a not in posts:
found = a
break
else:
print("No new posts found, exiting", flush=True)
exit()
print_and_run(f"git switch -f -C {q(args.branch2)}")
print_and_run("git config --global pull.rebase true")
print_and_run("git reset --hard")
print_and_run(f"git pull origin {q(args.branch2)} --depth 1 --force")
msg = (
f"New post: {found}\n{args.blogbase}/{found.replace('-', '/', 3)}.html"
)
print(msg, flush=True)
a = urllib.request.urlopen(
urllib.request.Request(
f"{args.instance}/api/v1/statuses",
headers={
"Authorization": "Bearer " + key,
"Content-Type": "application/json",
},
),
data=json.dumps(dict(status=msg)).encode("utf-8"),
)
d = a.read()
a.close()
g = json.loads(d)
if "id" not in g or "url" not in g:
print("error when posting", flush=True)
exit(g)
print(f"Posted with id {g['id']!r}", flush=True)
with open(post_file, "a") as f:
c = csv.writer(f, dialect="unix", quoting=csv.QUOTE_MINIMAL)
c.writerow([g["id"], found])
print_and_run(f"git add {q(post_file)}")
commit_msg = f"Update {post_file} with new post {g['id']}\n{found} {g['url']}"
print_and_run(f"git commit -m {q(commit_msg)}")
print_and_run(f"git push origin {q(args.branch2)} --force")