-
-
Save JoseHdez2/333aedc34dc5327f049d0c0e7a116883 to your computer and use it in GitHub Desktop.
Anki Deck Media Extractor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import io | |
import zipfile | |
import tempfile | |
from pathlib import Path | |
import sqlite3 | |
import json | |
import shutil | |
def write_json( file_path , python_object ): | |
with open( file_path , 'w', encoding='utf-8' ) as f: | |
json.dump( python_object , f , ensure_ascii=False , indent=4 ) | |
def read_json( file_path ): | |
with open( file_path ) as f: | |
return json.load( f ) | |
# https://stackoverflow.com/a/21387469 | |
def extract_media( apkg_file_path ): | |
input_file_path = Path( apkg_file_path ) | |
OUTPUT_DIR = input_file_path.parent.joinpath( f"{input_file_path.stem}-Media" ) | |
OUTPUT_DIR.mkdir( exist_ok=True , parents=True ) | |
with tempfile.TemporaryDirectory() as temp_dir: | |
temp_dir_posix = Path( temp_dir ) | |
archive = zipfile.ZipFile( apkg_file_path , 'r' ) | |
print( "Extracting .apkg File" ) | |
archive.extractall( path=temp_dir ) | |
media_json_file_path = temp_dir_posix.joinpath( "media" ) | |
media_info = read_json( str( media_json_file_path ) ) | |
print( "Copying Images" ) | |
for index , ( key , value ) in enumerate( media_info.items() ): | |
x_input_path = temp_dir_posix.joinpath( key ) | |
x_output_path = OUTPUT_DIR.joinpath( value ) | |
print( f"{index+1} === {key} === {value} === {x_output_path}" ) | |
shutil.copy( x_input_path , x_output_path ) | |
#https://www.reddit.com/r/Anki/comments/4j24ec/how_to_extract_the_contents_from_media_file/ | |
if __name__ == "__main__": | |
extract_media( sys.argv[ 1 ] ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment