@@ -116,16 +116,15 @@ def parse_video(self, video: Dict[str, Any]) -> str:
116
116
params ["duration" ] = self .parse_iso8601_duration (content_details ["duration" ])
117
117
# translate video to standard markdown
118
118
backslash_escaped_title = params ["title" ].replace ('"' , '\\ "' )
119
- result = f'[![{ params ["title" ]} ]({ self ._base_url } ?{ urllib .parse .urlencode (params )} "{ backslash_escaped_title } ")]({ video ["link" ]} )'
120
119
# if theme context is set, create two versions with theme context specified
121
120
if self ._theme_context_dark or self ._theme_context_light :
122
121
dark_params = params | self ._theme_context_dark
123
122
light_params = params | self ._theme_context_light
124
- result = (
123
+ return (
125
124
f'[![{ params ["title" ]} ]({ self ._base_url } ?{ urllib .parse .urlencode (dark_params )} "{ backslash_escaped_title } ")]({ video ["link" ]} #gh-dark-mode-only)'
126
125
f'[![{ params ["title" ]} ]({ self ._base_url } ?{ urllib .parse .urlencode (light_params )} "{ backslash_escaped_title } ")]({ video ["link" ]} #gh-light-mode-only)'
127
126
)
128
- return result . replace ( "/" , " \\ /" ). replace ( "'" , " \\ '" )
127
+ return f'[![ { params [ "title" ] } ]( { self . _base_url } ? { urllib . parse . urlencode ( params ) } " { backslash_escaped_title } ")]( { video [ "link" ] } )'
129
128
130
129
def parse_videos (self ) -> str :
131
130
"""Parse video feed and return the contents for the readme"""
@@ -136,6 +135,25 @@ def parse_videos(self) -> str:
136
135
return "\n " .join (map (self .parse_video , videos ))
137
136
138
137
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
+
139
157
if __name__ == "__main__" :
140
158
parser = ArgumentParser ()
141
159
parser .add_argument (
@@ -144,6 +162,12 @@ def parse_videos(self) -> str:
144
162
help = "YouTube channel ID" ,
145
163
required = True ,
146
164
)
165
+ parser .add_argument (
166
+ "--comment-tag-name" ,
167
+ dest = "comment_tag_name" ,
168
+ help = "Comment tag name" ,
169
+ default = "YOUTUBE-CARDS" ,
170
+ )
147
171
parser .add_argument (
148
172
"--max-videos" ,
149
173
dest = "max_videos" ,
@@ -207,6 +231,12 @@ def parse_videos(self) -> str:
207
231
default = "false" ,
208
232
choices = ("true" , "false" ),
209
233
)
234
+ parser .add_argument (
235
+ "--readme-path" ,
236
+ dest = "readme_path" ,
237
+ help = "Path to the readme file" ,
238
+ default = "README.md" ,
239
+ )
210
240
args = parser .parse_args ()
211
241
212
242
if args .show_duration == "true" and not args .youtube_api_key :
@@ -226,4 +256,10 @@ def parse_videos(self) -> str:
226
256
theme_context_dark = json .loads (args .theme_context_dark ),
227
257
)
228
258
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 )
0 commit comments