Skip to content
This repository was archived by the owner on Oct 29, 2022. It is now read-only.

Commit ea46291

Browse files
authored
Merge pull request #48 from kjaymiller/kjaymiller/issue10
Base Automation Scripts for GH Actions to Use
2 parents 6780e88 + 0b6b8be commit ea46291

25 files changed

+327
-76
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ venv.bak/
118118
.ropeproject
119119

120120
# mkdocs documentation
121-
/site
122-
123121
# mypy
124122
.mypy_cache/
125123
.dmypy.json

app/src/build_issues.py

Lines changed: 0 additions & 45 deletions
This file was deleted.

automation/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import automation.engine as engine
2+
import automation.issues as issues
3+
import automation.newsletter as newsletter

automation/archive.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""Takes Issues Filed in the last week and puts them in a file for the upcoming stream"""
2+
import os
3+
import pathlib
4+
from datetime import datetime, timedelta
5+
6+
from jinja2 import Environment, FileSystemLoader
7+
8+
from engine import engine
9+
from issues import get_issues_from_github
10+
11+
# Get the date of the next friday
12+
13+
14+
def create_post(*, filename: str | pathlib.Path, template: str, **metadata) -> int:
15+
"""Creates a markdown document for this week for render_engine to process"""
16+
filename = pathlib.Path("app/content").joinpath(pathlib.Path(filename))
17+
template = engine.get_template(template)
18+
return filename.write_text(template.render(metadata))
19+
20+
21+
def get_show_file(directory: pathlib.Path, date: datetime.datetime) -> pathlib.Path:
22+
"""Get the file for a show on a given date"""
23+
show_date = date.strftime("%Y-%m-%d")
24+
show_file = pathlib.Path(directory).joinpath(show_date).with_suffix(".md")
25+
return show_file

automation/engine.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from jinja2 import Environment, FileSystemLoader
2+
3+
engine = Environment(loader=FileSystemLoader(["templates", "automation/templates"]))

automation/issues.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import datetime
2+
import re
3+
from collections import defaultdict
4+
from typing import Generator
5+
6+
import httpx
7+
from markdown_it import MarkdownIt
8+
from markdown_it.tree import SyntaxTreeNode
9+
10+
11+
def get_issue(issue_id: str) -> dict[str, str]:
12+
"""Returns the issue with the given id"""
13+
url = f"https://api.github.com/repos/kjaymiller/Python-Community-News/issues/{issue_id}" # TODO: remove hardcoded issue url
14+
request = httpx.get(url)
15+
return request.json()
16+
17+
18+
def get_issues(labels, since_date: datetime.datetime | None) -> list:
19+
"""Returns the issues filed in the last week"""
20+
url = "https://api.github.com/repos/kjaymiller/Python-Community-News/issues"
21+
params = {"labels": ",".join(labels), "since": since_date}
22+
request = httpx.get(url, params=params)
23+
return request.json()
24+
25+
26+
def parse_issue_markdown(text) -> dict:
27+
"""Use markdownit to split at section headings"""
28+
md = MarkdownIt("zero", {"maxNesting": 1})
29+
md.enable(["heading", "paragraph"])
30+
tokens = md.parse(text)
31+
node = SyntaxTreeNode(tokens)
32+
issue_object = defaultdict(list)
33+
for n in node.children:
34+
if n.type == "heading":
35+
issue_key = n.children[0].content
36+
elif content := n.children[0].content == "_No response_":
37+
continue
38+
else:
39+
issue_object[issue_key].append(n.children[0].content)
40+
return issue_object
41+
42+
43+
def get_content_issues(body, issues_tag: str) -> Generator[dict[str, str], None, None]:
44+
"""
45+
Loads the issues from the file and returns the template show the newsletter.
46+
"""
47+
md = parse_issue_markdown(body)
48+
49+
if issues_tag not in md:
50+
raise ValueError(f"{issues_tag} is required in the issue")
51+
52+
issues = re.findall(r"\d+", md[issues_tag][0])
53+
return issues

automation/newsletter.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import datetime
2+
import os
3+
import pathlib
4+
from typing import Generator
5+
6+
import frontmatter
7+
import httpx
8+
9+
hour = str
10+
minute = str
11+
buttondown_api_key = os.getenv("BUTTONDOWN_API_KEY")
12+
header = {"Authorization": f"Token {buttondown_api_key}"}
13+
14+
schedule_email_url = "https://api.buttondown.email/v1/scheduled-emails"
15+
16+
17+
def get_publish_time(
18+
date: datetime.datetime,
19+
time: datetime.time,
20+
) -> datetime.datetime:
21+
"""Helper for getting the date and time for when to schedule the email"""
22+
return datetime.datetime.combine(date, time)
23+
24+
25+
def build_email_from_content(
26+
filepath: pathlib.Path,
27+
) -> dict[str, str]:
28+
"""
29+
Uses frontmatter to get the subject and returns a partial of the body to be used to schedule the email.
30+
"""
31+
content = frontmatter.load(filepath)
32+
subject = content.metadata.get("subject")
33+
body = {
34+
"email_type": "public",
35+
"body": content.content,
36+
"subject": subject,
37+
}
38+
return body
39+
40+
41+
def schedule_email_from_post(
42+
body: dict[str, str],
43+
publish_date: datetime.datetime,
44+
) -> httpx.Response:
45+
"""
46+
Returns the status code of the request.
47+
The the `publish_date` time are required to schedule the email.
48+
"""
49+
50+
body.update({"publish_date": publish_date.isoformat()})
51+
request = httpx.post(
52+
schedule_email_url,
53+
headers=header,
54+
json=body,
55+
)
56+
return request

templates/content_gen/episode_template.md renamed to automation/templates/archive.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22
title: Weekly Community News - {{date}}
33
date: {{date}}
44
tags: {{tags}}
5+
youtube: {{youtube}}
6+
podcast: {{podcast}}
57
github: https://github.com/kjaymiller/Python-Community-News/blob/main/app/content/{{date}}.md
68
---
79

10+
## Topics
811
{% for issue in issues %}
9-
# {{issue.title}}
12+
### {{issue.title}}
1013

1114
<small>Submitted by: [{{issue.user.login}}]({{issue.user.url}}) on {{issue.created_at}}</small>
1215

1316
{{issue.body}}
1417

1518
{% endfor %}
19+
20+
## Open CFPs and Upcoming Conferences
21+
### CFPs Open
22+
{% for cfp in cfps %}
23+
- [{{cfp.title}}]({{cfp.url}})
24+
{% endfor %}
25+
26+
### Conferences
27+
{% for conference in conferences %}
28+
- [{{conference.title}}]({{conference.url}})
29+
{% endfor %}

automation/templates/newsletter.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
subject: Weekly Community News - {{date}}
3+
github: https://github.com/kjaymiller/Python-Community-News/blob/main/site/content/{{date}}.md
4+
youtube: {{youtube}}
5+
podcast: {{podcast}}
6+
---
7+
8+
## Topics
9+
{% for issue in issues %}
10+
### {{issue.title}}
11+
12+
<small>Submitted by: [{{issue.user.login}}]({{issue.user.url}}) on {{issue.created_at}}</small>
13+
14+
{{issue.body}}
15+
{% endfor %}
16+
17+
### Watch the VOD on Youtube:
18+
https://youtube.com/watch/{{youtube}}
19+
20+
### Take the content on the road with you!
21+
https://transistor.fm/s/{{podcast}}
File renamed without changes.

0 commit comments

Comments
 (0)