Add Channel.set_download_directory.
This commit is contained in:
		
							parent
							
								
									38299db102
								
							
						
					
					
						commit
						2ad85ad69a
					
				
					 5 changed files with 87 additions and 5 deletions
				
			
		|  | @ -2,6 +2,7 @@ import flask; from flask import request | ||||||
| import itertools | import itertools | ||||||
| 
 | 
 | ||||||
| from voussoirkit import flasktools | from voussoirkit import flasktools | ||||||
|  | from voussoirkit import pathclass | ||||||
| 
 | 
 | ||||||
| import ycdl | import ycdl | ||||||
| 
 | 
 | ||||||
|  | @ -157,6 +158,22 @@ def post_set_automark(channel_id): | ||||||
| 
 | 
 | ||||||
|     return flasktools.make_json_response({}) |     return flasktools.make_json_response({}) | ||||||
| 
 | 
 | ||||||
|  | @site.route('/channel/<channel_id>/set_download_directory', methods=['POST']) | ||||||
|  | def post_set_download_directory(channel_id): | ||||||
|  |     download_directory = request.form['download_directory'] | ||||||
|  |     channel = common.ycdldb.get_channel(channel_id) | ||||||
|  | 
 | ||||||
|  |     try: | ||||||
|  |         channel.set_download_directory(download_directory) | ||||||
|  |     except pathclass.NotDirectory: | ||||||
|  |         exc = { | ||||||
|  |             'error_type': 'NOT_DIRECTORY', | ||||||
|  |             'error_message': f'"{download_directory}" is not a directory.', | ||||||
|  |         } | ||||||
|  |         return flasktools.make_json_response(exc, status=400) | ||||||
|  | 
 | ||||||
|  |     return flasktools.make_json_response({}) | ||||||
|  | 
 | ||||||
| @site.route('/channel/<channel_id>/set_queuefile_extension', methods=['POST']) | @site.route('/channel/<channel_id>/set_queuefile_extension', methods=['POST']) | ||||||
| def post_set_queuefile_extension(channel_id): | def post_set_queuefile_extension(channel_id): | ||||||
|     extension = request.form['extension'] |     extension = request.form['extension'] | ||||||
|  |  | ||||||
|  | @ -47,6 +47,15 @@ function set_automark(channel_id, state, callback) | ||||||
|     return common.post(url, data, callback); |     return common.post(url, data, callback); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | api.channels.set_download_directory = | ||||||
|  | function set_download_directory(channel_id, download_directory, callback) | ||||||
|  | { | ||||||
|  |     const url = `/channel/${channel_id}/set_download_directory`; | ||||||
|  |     const data = new FormData(); | ||||||
|  |     data.append("download_directory", download_directory); | ||||||
|  |     return common.post(url, data, callback); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| api.channels.set_queuefile_extension = | api.channels.set_queuefile_extension = | ||||||
| function set_queuefile_extension(channel_id, extension, callback) | function set_queuefile_extension(channel_id, extension, callback) | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -274,6 +274,11 @@ https://stackoverflow.com/a/35153397 | ||||||
|         <button id="set_queuefile_extension_button" class="button_with_spinner" onclick="return set_queuefile_extension_form();">Set extension</button> |         <button id="set_queuefile_extension_button" class="button_with_spinner" onclick="return set_queuefile_extension_form();">Set extension</button> | ||||||
|     </div> |     </div> | ||||||
| 
 | 
 | ||||||
|  |     <div> | ||||||
|  |         <input type="text" id="set_download_directory_input" placeholder="Queuefile directory" value="{{channel.download_directory or ''}}"/> | ||||||
|  |         <button id="set_download_directory_button" class="button_with_spinner" onclick="return set_download_directory_form();">Set directory</button> | ||||||
|  |     </div> | ||||||
|  | 
 | ||||||
|     <div><a href="https://www.youtube.com/feeds/videos.xml?channel_id={{channel.id}}">Channel RSS</a></div> |     <div><a href="https://www.youtube.com/feeds/videos.xml?channel_id={{channel.id}}">Channel RSS</a></div> | ||||||
| 
 | 
 | ||||||
|     <button class="red_button button_with_confirm" |     <button class="red_button button_with_confirm" | ||||||
|  | @ -610,6 +615,21 @@ function set_automark_callback(response) | ||||||
|     set_automark_spinner.hide(); |     set_automark_spinner.hide(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | function set_download_directory_form(event) | ||||||
|  | { | ||||||
|  |     const download_directory = set_download_directory_input.value.trim(); | ||||||
|  |     api.channels.set_download_directory(CHANNEL_ID, download_directory, set_download_directory_callback); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | function set_download_directory_callback(response) | ||||||
|  | { | ||||||
|  |     if (response.meta.status != 200) | ||||||
|  |     { | ||||||
|  |         alert(JSON.stringify(response)); | ||||||
|  |     } | ||||||
|  |     window[set_download_directory_button.dataset.spinnerCloser](); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| function set_queuefile_extension_form(event) | function set_queuefile_extension_form(event) | ||||||
| { | { | ||||||
|     const extension = set_queuefile_extension_input.value.trim(); |     const extension = set_queuefile_extension_input.value.trim(); | ||||||
|  |  | ||||||
|  | @ -1,6 +1,7 @@ | ||||||
| import datetime | import datetime | ||||||
| import typing | import typing | ||||||
| 
 | 
 | ||||||
|  | from voussoirkit import pathclass | ||||||
| 
 | 
 | ||||||
| from . import constants | from . import constants | ||||||
| from . import exceptions | from . import exceptions | ||||||
|  | @ -35,13 +36,38 @@ class Channel(Base): | ||||||
|         self.id = db_row['id'] |         self.id = db_row['id'] | ||||||
|         self.name = db_row['name'] |         self.name = db_row['name'] | ||||||
|         self.uploads_playlist = db_row['uploads_playlist'] |         self.uploads_playlist = db_row['uploads_playlist'] | ||||||
|         self.download_directory = db_row['download_directory'] |         self.download_directory = self.normalize_download_directory( | ||||||
|  |             db_row['download_directory'], | ||||||
|  |             do_assert=False, | ||||||
|  |         ) | ||||||
|         self.queuefile_extension = self.normalize_queuefile_extension(db_row['queuefile_extension']) |         self.queuefile_extension = self.normalize_queuefile_extension(db_row['queuefile_extension']) | ||||||
|         self.automark = db_row['automark'] or 'pending' |         self.automark = db_row['automark'] or 'pending' | ||||||
| 
 | 
 | ||||||
|     def __repr__(self): |     def __repr__(self): | ||||||
|         return f'Channel:{self.id}' |         return f'Channel:{self.id}' | ||||||
| 
 | 
 | ||||||
|  |     @staticmethod | ||||||
|  |     def normalize_download_directory( | ||||||
|  |             download_directory, | ||||||
|  |             do_assert=True, | ||||||
|  |         ) -> typing.Optional[pathclass.Path]: | ||||||
|  |         if download_directory is None: | ||||||
|  |             return None | ||||||
|  | 
 | ||||||
|  |         if not isinstance(download_directory, (str, pathclass.Path)): | ||||||
|  |             raise TypeError(f'download_directory should be {str} or {pathclass.Path}, not {type(download_directory)}.') | ||||||
|  | 
 | ||||||
|  |         if isinstance(download_directory, str): | ||||||
|  |             download_directory = download_directory.strip() | ||||||
|  |             if not download_directory: | ||||||
|  |                 return None | ||||||
|  | 
 | ||||||
|  |         download_directory = pathclass.Path(download_directory) | ||||||
|  |         if do_assert: | ||||||
|  |             download_directory.assert_is_directory() | ||||||
|  | 
 | ||||||
|  |         return download_directory | ||||||
|  | 
 | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def normalize_queuefile_extension(queuefile_extension) -> typing.Optional[str]: |     def normalize_queuefile_extension(queuefile_extension) -> typing.Optional[str]: | ||||||
|         if queuefile_extension is None: |         if queuefile_extension is None: | ||||||
|  | @ -178,6 +204,18 @@ class Channel(Base): | ||||||
|         if commit: |         if commit: | ||||||
|             self.ycdldb.commit() |             self.ycdldb.commit() | ||||||
| 
 | 
 | ||||||
|  |     def set_download_directory(self, download_directory, commit=True): | ||||||
|  |         download_directory = self.normalize_download_directory(download_directory) | ||||||
|  | 
 | ||||||
|  |         pairs = { | ||||||
|  |             'id': self.id, | ||||||
|  |             'download_directory': download_directory.absolute_path if download_directory else None, | ||||||
|  |         } | ||||||
|  |         self.ycdldb.sql_update(table='channels', pairs=pairs, where_key='id') | ||||||
|  |         self.download_directory = download_directory | ||||||
|  | 
 | ||||||
|  |         if commit: | ||||||
|  |             self.ycdldb.commit() | ||||||
| 
 | 
 | ||||||
|     def set_queuefile_extension(self, queuefile_extension, commit=True): |     def set_queuefile_extension(self, queuefile_extension, commit=True): | ||||||
|         queuefile_extension = self.normalize_queuefile_extension(queuefile_extension) |         queuefile_extension = self.normalize_queuefile_extension(queuefile_extension) | ||||||
|  |  | ||||||
|  | @ -135,9 +135,7 @@ class YCDLDBChannelMixin: | ||||||
|         if name is None: |         if name is None: | ||||||
|             name = self.youtube.get_user_name(channel_id) |             name = self.youtube.get_user_name(channel_id) | ||||||
| 
 | 
 | ||||||
|         if download_directory is not None: |         download_directory = objects.Channel.normalize_download_directory(download_directory) | ||||||
|             download_directory = pathclass.Path(download_directory).absolute_path |  | ||||||
| 
 |  | ||||||
|         queuefile_extension = objects.Channel.normalize_queuefile_extension(queuefile_extension) |         queuefile_extension = objects.Channel.normalize_queuefile_extension(queuefile_extension) | ||||||
| 
 | 
 | ||||||
|         self.log.info('Adding channel %s %s', channel_id, name) |         self.log.info('Adding channel %s %s', channel_id, name) | ||||||
|  | @ -146,7 +144,7 @@ class YCDLDBChannelMixin: | ||||||
|             'id': channel_id, |             'id': channel_id, | ||||||
|             'name': name, |             'name': name, | ||||||
|             'uploads_playlist': self.youtube.get_user_uploads_playlist_id(channel_id), |             'uploads_playlist': self.youtube.get_user_uploads_playlist_id(channel_id), | ||||||
|             'download_directory': download_directory, |             'download_directory': download_directory.absolute_path if download_directory else None, | ||||||
|             'queuefile_extension': queuefile_extension, |             'queuefile_extension': queuefile_extension, | ||||||
|             'automark': "pending", |             'automark': "pending", | ||||||
|         } |         } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue