Merge pull request #142 from mauamy/add-url-download-to-storeMediaFile

Add url download option to storeMediaFile
This commit is contained in:
Alex Yatskov 2020-03-13 17:30:33 -07:00 committed by GitHub
commit 5ca78ea40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 7 deletions

View File

@ -1494,9 +1494,10 @@ guarantee that your application continues to function properly in the future.
* **storeMediaFile** * **storeMediaFile**
Stores a file with the specified base64-encoded contents inside the media folder. To prevent Anki from removing Stores a file with the specified base64-encoded contents inside the media folder. alternatively you can specify a
files not used by any cards (e.g. for configuration files), prefix the filename with an underscore. These files are url from where the file shell be downloaded. If both field `data` and `url` are provided, the `data` field will be
still synchronized to AnkiWeb. used. To prevent Anki from removing files not used by any cards (e.g. for configuration files), prefix the filename
with an underscore. These files are still synchronized to AnkiWeb.
*Sample request*: *Sample request*:
```json ```json
@ -1523,6 +1524,26 @@ guarantee that your application continues to function properly in the future.
Hello world! Hello world!
``` ```
*Sample request*:
```json
{
"action": "storeMediaFile",
"version": 6,
"params": {
"filename": "_hello.txt",
"url": "https://url.to.file"
}
}
```
*Sample result*:
```json
{
"result": null,
"error": null
}
```
* **retrieveMediaFile** * **retrieveMediaFile**
Retrieves the base64-encoded contents of the specified file, returning `false` if the file does not exist. Retrieves the base64-encoded contents of the specified file, returning `false` if the file does not exist.

View File

@ -421,10 +421,15 @@ class AnkiConnect:
@util.api() @util.api()
def storeMediaFile(self, filename, data): def storeMediaFile(self, filename, data=None, url=None):
if data:
self.deleteMediaFile(filename) self.deleteMediaFile(filename)
self.media().writeData(filename, base64.b64decode(data)) self.media().writeData(filename, base64.b64decode(data))
elif url:
downloadedData = util.download(url)
self.media().writeData(filename, downloadedData)
else:
raise Exception('You must either provide a "data" or a "url" field.')
@util.api() @util.api()
def retrieveMediaFile(self, filename): def retrieveMediaFile(self, filename):