Skip to content

Commit 3da484e

Browse files
authored
feat: Add option for readme path and refactor readme update (#16)
1 parent a2e0b2f commit 3da484e

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

Diff for: action.py

+40-4
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,15 @@ def parse_video(self, video: Dict[str, Any]) -> str:
116116
params["duration"] = self.parse_iso8601_duration(content_details["duration"])
117117
# translate video to standard markdown
118118
backslash_escaped_title = params["title"].replace('"', '\\"')
119-
result = f'[![{params["title"]}]({self._base_url}?{urllib.parse.urlencode(params)} "{backslash_escaped_title}")]({video["link"]})'
120119
# if theme context is set, create two versions with theme context specified
121120
if self._theme_context_dark or self._theme_context_light:
122121
dark_params = params | self._theme_context_dark
123122
light_params = params | self._theme_context_light
124-
result = (
123+
return (
125124
f'[![{params["title"]}]({self._base_url}?{urllib.parse.urlencode(dark_params)} "{backslash_escaped_title}")]({video["link"]}#gh-dark-mode-only)'
126125
f'[![{params["title"]}]({self._base_url}?{urllib.parse.urlencode(light_params)} "{backslash_escaped_title}")]({video["link"]}#gh-light-mode-only)'
127126
)
128-
return result.replace("/", "\\/").replace("'", "\\'")
127+
return f'[![{params["title"]}]({self._base_url}?{urllib.parse.urlencode(params)} "{backslash_escaped_title}")]({video["link"]})'
129128

130129
def parse_videos(self) -> str:
131130
"""Parse video feed and return the contents for the readme"""
@@ -136,6 +135,25 @@ def parse_videos(self) -> str:
136135
return "\n".join(map(self.parse_video, videos))
137136

138137

138+
class FileUpdater:
139+
"""Update the readme file"""
140+
141+
@staticmethod
142+
def update(readme_path: str, comment_tag: str, replace_content: str):
143+
"""Replace the text between the begin and end tags with the replace content"""
144+
begin_tag = f"<!-- BEGIN {comment_tag} -->"
145+
end_tag = f"<!-- END {comment_tag} -->"
146+
with open(readme_path, "r") as readme_file:
147+
readme = readme_file.read()
148+
begin_index = readme.find(begin_tag)
149+
end_index = readme.find(end_tag)
150+
if begin_index == -1 or end_index == -1:
151+
raise RuntimeError(f"Could not find tags {begin_tag} and {end_tag} in {readme_path}")
152+
readme = f"{readme[:begin_index + len(begin_tag)]}\n{replace_content}\n{readme[end_index:]}"
153+
with open(readme_path, "w") as readme_file:
154+
readme_file.write(readme)
155+
156+
139157
if __name__ == "__main__":
140158
parser = ArgumentParser()
141159
parser.add_argument(
@@ -144,6 +162,12 @@ def parse_videos(self) -> str:
144162
help="YouTube channel ID",
145163
required=True,
146164
)
165+
parser.add_argument(
166+
"--comment-tag-name",
167+
dest="comment_tag_name",
168+
help="Comment tag name",
169+
default="YOUTUBE-CARDS",
170+
)
147171
parser.add_argument(
148172
"--max-videos",
149173
dest="max_videos",
@@ -207,6 +231,12 @@ def parse_videos(self) -> str:
207231
default="false",
208232
choices=("true", "false"),
209233
)
234+
parser.add_argument(
235+
"--readme-path",
236+
dest="readme_path",
237+
help="Path to the readme file",
238+
default="README.md",
239+
)
210240
args = parser.parse_args()
211241

212242
if args.show_duration == "true" and not args.youtube_api_key:
@@ -226,4 +256,10 @@ def parse_videos(self) -> str:
226256
theme_context_dark=json.loads(args.theme_context_dark),
227257
)
228258

229-
print(video_parser.parse_videos())
259+
video_content = video_parser.parse_videos()
260+
261+
# output to stdout
262+
print(video_content)
263+
264+
# update the readme file
265+
FileUpdater.update(args.readme_path, args.comment_tag_name, video_content)

Diff for: action.yml

+19-13
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ inputs:
6565
description: "The commit message to use for the commit"
6666
required: false
6767
default: "docs(readme): Update YouTube cards"
68+
readme_path:
69+
description: "The path to the readme file"
70+
required: false
71+
default: "README.md"
6872

6973
runs:
7074
using: "composite"
@@ -84,20 +88,22 @@ runs:
8488
- name: Generate Readme Update
8589
id: "generate-readme-update"
8690
shell: bash
87-
run: echo ::set-output name=readme_update::$(python ${{ github.action_path }}/action.py --channel "${{ inputs.channel_id }}" --max-videos ${{ inputs.max_videos }} --base-url "${{ inputs.base_url }}" --card-width ${{ inputs.card_width }} --background-color "${{ inputs.background_color }}" --title-color "${{ inputs.title_color }}" --stats-color "${{ inputs.stats_color }}" --youtube-api-key "${{ inputs.youtube_api_key }}" --show-duration "${{ inputs.show_duration }}" --theme-context-light '${{ inputs.theme_context_light }}' --theme-context-dark '${{ inputs.theme_context_dark }}')
88-
89-
- name: Update README
90-
shell: bash
91-
env:
92-
BEGIN_TAG: "<!-- BEGIN ${{ inputs.comment_tag_name }} -->"
93-
END_TAG: "<!-- END ${{ inputs.comment_tag_name }} -->"
9491
run: |
95-
if [ -z '${{ steps.generate-readme-update.outputs.readme_update }}' ]; then
96-
echo "Python script finished with no output. Exiting with error."
97-
exit 1
98-
fi
99-
UPDATE=$(cat README.md | perl -0777 -pe 's/(${{ env.BEGIN_TAG }})(?:.|\n)*?(${{ env.END_TAG }})/${1}\n${{ steps.generate-readme-update.outputs.readme_update }}\n${2}/g')
100-
echo "${UPDATE}" > README.md
92+
UPDATE=$(python ${{ github.action_path }}/action.py \
93+
--channel "${{ inputs.channel_id }}" \
94+
--comment-tag-name "${{ inputs.comment_tag_name }}" \
95+
--max-videos ${{ inputs.max_videos }} \
96+
--base-url "${{ inputs.base_url }}" \
97+
--card-width ${{ inputs.card_width }} \
98+
--background-color "${{ inputs.background_color }}" \
99+
--title-color "${{ inputs.title_color }}" \
100+
--stats-color "${{ inputs.stats_color }}" \
101+
--youtube-api-key "${{ inputs.youtube_api_key }}" \
102+
--show-duration "${{ inputs.show_duration }}" \
103+
--theme-context-light '${{ inputs.theme_context_light }}' \
104+
--theme-context-dark '${{ inputs.theme_context_dark }}' \
105+
--readme-path "${{ inputs.readme_path }}" \
106+
) || exit 1
101107
102108
- name: Commit changes
103109
uses: EndBug/add-and-commit@v9

0 commit comments

Comments
 (0)