Allow to add media to notes by file path or base64 encoded data (#230)
* Allow to add media to notes by file path or base64 encoded data * Adjust Media Actions documentation * Adjust Note Actions documentation
This commit is contained in:
parent
4d2ab991c2
commit
7ef10e92da
@ -20,7 +20,7 @@
|
|||||||
*Sample result*:
|
*Sample result*:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"result": null,
|
"result": "_hello.txt",
|
||||||
"error": null
|
"error": null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -45,7 +45,7 @@
|
|||||||
*Sample result*:
|
*Sample result*:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"result": null,
|
"result": "_hello.txt",
|
||||||
"error": null
|
"error": null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@ -65,7 +65,7 @@
|
|||||||
*Sample result*:
|
*Sample result*:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"result": null,
|
"result": "_hello.txt",
|
||||||
"error": null
|
"error": null
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -7,9 +7,10 @@
|
|||||||
|
|
||||||
AnkiConnect can download audio, video, and picture files and embed them in newly created notes. The corresponding `audio`, `video`, and `picture` note members are
|
AnkiConnect can download audio, video, and picture files and embed them in newly created notes. The corresponding `audio`, `video`, and `picture` note members are
|
||||||
optional and can be omitted. If you choose to include any of them, they should contain a single object or an array of objects
|
optional and can be omitted. If you choose to include any of them, they should contain a single object or an array of objects
|
||||||
with mandatory `url` and `filename` fields. The `skipHash` field can be optionally provided to skip the inclusion of
|
with the mandatory `filename` field and one of `data`, `path` or `url`. Refer to the documentation of `storeMediaFile` for an explanation of these fields.
|
||||||
downloaded files with an MD5 hash that matches the provided value. This is useful for avoiding the saving of error
|
The `skipHash` field can be optionally provided to skip the inclusion of files with an MD5 hash that matches the provided value.
|
||||||
pages and stub files. The `fields` member is a list of fields that should play audio or video, or show a picture when the card is displayed in
|
This is useful for avoiding the saving of error pages and stub files.
|
||||||
|
The `fields` member is a list of fields that should play audio or video, or show a picture when the card is displayed in
|
||||||
Anki. The `allowDuplicate` member inside `options` group can be set to true to enable adding duplicate cards.
|
Anki. The `allowDuplicate` member inside `options` group can be set to true to enable adding duplicate cards.
|
||||||
Normally duplicate cards can not be added and trigger exception.
|
Normally duplicate cards can not be added and trigger exception.
|
||||||
|
|
||||||
|
@ -484,22 +484,32 @@ class AnkiConnect:
|
|||||||
|
|
||||||
|
|
||||||
@util.api()
|
@util.api()
|
||||||
def storeMediaFile(self, filename, data=None, path=None, url=None):
|
def storeMediaFile(self, filename, data=None, path=None, url=None, skipHash=None):
|
||||||
if data:
|
if data:
|
||||||
self.deleteMediaFile(filename)
|
self.deleteMediaFile(filename)
|
||||||
self.media().writeData(filename, base64.b64decode(data))
|
mediaData = base64.b64decode(data)
|
||||||
elif path:
|
elif path:
|
||||||
self.deleteMediaFile(filename)
|
self.deleteMediaFile(filename)
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
data = f.read()
|
mediaData = f.read()
|
||||||
self.media().writeData(filename, data)
|
|
||||||
elif url:
|
elif url:
|
||||||
self.deleteMediaFile(filename)
|
self.deleteMediaFile(filename)
|
||||||
downloadedData = util.download(url)
|
mediaData = util.download(url)
|
||||||
self.media().writeData(filename, downloadedData)
|
|
||||||
else:
|
else:
|
||||||
raise Exception('You must either provide a "data" or a "url" field.')
|
raise Exception('You must either provide a "data" or a "url" field.')
|
||||||
|
|
||||||
|
if skipHash is None:
|
||||||
|
skip = False
|
||||||
|
else:
|
||||||
|
m = hashlib.md5()
|
||||||
|
m.update(data)
|
||||||
|
skip = skipHash == m.hexdigest()
|
||||||
|
|
||||||
|
if skip:
|
||||||
|
return None
|
||||||
|
return self.media().writeData(filename, mediaData)
|
||||||
|
|
||||||
|
|
||||||
@util.api()
|
@util.api()
|
||||||
def retrieveMediaFile(self, filename):
|
def retrieveMediaFile(self, filename):
|
||||||
filename = os.path.basename(filename)
|
filename = os.path.basename(filename)
|
||||||
@ -558,17 +568,13 @@ class AnkiConnect:
|
|||||||
for media in mediaList:
|
for media in mediaList:
|
||||||
if media is not None and len(media['fields']) > 0:
|
if media is not None and len(media['fields']) > 0:
|
||||||
try:
|
try:
|
||||||
data = util.download(media['url'])
|
mediaFilename = self.storeMediaFile(media['filename'],
|
||||||
skipHash = media.get('skipHash')
|
data=media.get('data'),
|
||||||
if skipHash is None:
|
path=media.get('path'),
|
||||||
skip = False
|
url=media.get('url'),
|
||||||
else:
|
skipHash=media.get('skipHash'))
|
||||||
m = hashlib.md5()
|
|
||||||
m.update(data)
|
|
||||||
skip = skipHash == m.hexdigest()
|
|
||||||
|
|
||||||
if not skip:
|
if mediaFilename is not None:
|
||||||
mediaFilename = self.media().writeData(media['filename'], data)
|
|
||||||
for field in media['fields']:
|
for field in media['fields']:
|
||||||
if field in ankiNote:
|
if field in ankiNote:
|
||||||
if mediaType is util.MediaType.Picture:
|
if mediaType is util.MediaType.Picture:
|
||||||
|
Loading…
Reference in New Issue
Block a user