diff --git a/environment.yml b/environment.yml index d22860c..e07a14f 100644 --- a/environment.yml +++ b/environment.yml @@ -1,4 +1,4 @@ -name: vocab-to-anki-app +name: vocab-to-anki channels: - conda-forge dependencies: @@ -13,4 +13,5 @@ dependencies: - ibm-watson - kivy[full] - kivymd + - lxml - user_agent \ No newline at end of file diff --git a/main.py b/main.py index b1189d9..87e3232 100644 --- a/main.py +++ b/main.py @@ -47,6 +47,9 @@ # # # + +# pyinstaller --noconfirm --onedir --windowed --add-data "C:/Users/Standard User/miniconda3/envs/vocab-to-anki-app/Lib/site-packages/user_agent/data;user_agent/data/" "C:/Users/Public/Documents/projects/python/vocab-to-anki/main.py" + import os from glob import glob @@ -64,12 +67,13 @@ def main(): MyApp().get_running_app().db_connection.close() # Delete Files on exit. print('Cleaning up..') - root_path = get_root_path() - mp3_files = glob(root_path + 'media/*.mp3') + media_path = get_root_path(media=True) + mp3_files = glob(os.path.join(media_path, '*.mp3')) for f in mp3_files: os.remove(f) - if os.path.exists(root_path + 'output.apkg'): - os.remove(root_path + 'output.apkg') + root_path = get_root_path() + if os.path.exists(os.path.join(root_path, 'output.apkg')): + os.remove(os.path.join(root_path, 'output.apkg')) print('Cleaned.') diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index c3ecfca..0000000 Binary files a/requirements.txt and /dev/null differ diff --git a/src/__pycache__/app.cpython-38.pyc b/src/__pycache__/app.cpython-38.pyc index fba66f4..172ee5f 100644 Binary files a/src/__pycache__/app.cpython-38.pyc and b/src/__pycache__/app.cpython-38.pyc differ diff --git a/src/app.py b/src/app.py index 39afb38..988cafe 100644 --- a/src/app.py +++ b/src/app.py @@ -59,8 +59,7 @@ update_globals_var, select_global_var_value, create_globals_var, select_all_decks, select_decks_which_contains, \ create_deck, select_deck from src.dict_scraper.spiders import cambridge -from src.lib.helpers import get_root_path, is_platform, check_android_permissions, request_android_permissions, \ - get_db_path +from src.lib.helpers import get_root_path, is_platform, check_android_permissions, request_android_permissions from src.lib.json_to_apkg import JsonToApkg from src.lib.strings import get_text @@ -331,14 +330,13 @@ def close_dialog(self, obj): self.dialog.dismiss() def open_apkg(self, obj): - path = get_root_path() - apkg_filename = 'output' + '.apkg' + file_path = os.path.join(get_root_path(), 'output.apkg') # print(apkg_filename) # if platform.system() == 'Darwin': # macOS # subprocess.call(('open', path + apkg_filename)) if is_platform('win'): # Windows - os.startfile(os.path.join(path[:-1], apkg_filename)) + os.startfile(file_path) else: # linux variants try: from jnius import cast @@ -355,7 +353,7 @@ def open_apkg(self, obj): currentActivity = cast('android.app.Activity', PythonActivity.mActivity) ctx = currentActivity.getApplicationContext() - filePath = File(path + apkg_filename) + filePath = File(file_path) uriApkg = FileProvider.getUriForFile(ctx, f"{ctx.getPackageName()}.file_provider", filePath) urifromfile = Uri.fromFile(filePath) parcelable = cast('android.os.Parcelable', urifromfile) @@ -853,7 +851,7 @@ def create_tables(self): return False if self.db_connection is not None: return True - db_path = f'{get_db_path()}data.db' + db_path = os.path.join(get_root_path(db=True), 'data.db') self.db_connection = create_connection(db_path) # https://stackoverflow.com/a/44951682 sql_create_tags_table = """ diff --git a/src/dict_scraper/spiders/cambridge.py b/src/dict_scraper/spiders/cambridge.py index d16a357..1e92a41 100644 --- a/src/dict_scraper/spiders/cambridge.py +++ b/src/dict_scraper/spiders/cambridge.py @@ -344,7 +344,7 @@ def parse(self): # us_pronunciation = response.css(".us #ampaudio2 source::attr(src)").extract_first() # amp-audio def download_audio(text: str, text_type: str = '') -> str: - root_path = get_root_path() + 'media/' + media_path = get_root_path(media=True) if text_type: filename = get_valid_filename(word + f'_{text_type}_' + lang + '.mp3') else: @@ -357,7 +357,7 @@ def download_audio(text: str, text_type: str = '') -> str: ibm_endpoint_url = self.ibm_tuple[2] ibm_tts = ibm_tts_authenticate(ibm_api_id, ibm_endpoint_url) watson_voice = get_voice(ibm_tts, gender, watson_lang) - with open(root_path + filename, 'wb') as audio_file: + with open(os.path.join(media_path, filename), 'wb') as audio_file: res = ibm_tts.synthesize(text, accept='audio/mp3', voice=watson_voice['name']).get_result() audio_file.write(res.content) except: @@ -365,8 +365,8 @@ def download_audio(text: str, text_type: str = '') -> str: tts = gTTS(text, lang='en', tld=gtts_tld) except: tts = gTTS(word, lang='en', tld=gtts_tld) - if not os.path.exists(root_path + filename): - tts.save(root_path + filename) + if not os.path.exists(os.path.join(media_path, filename)): + tts.save(os.path.join(media_path, filename)) # url = 'https://' + CambridgeSpider.allowed_domains[0] + address # http = urllib3.PoolManager(10, headers={'user-agent': USER_AGENT}) diff --git a/src/lib/__pycache__/json_to_apkg.cpython-38.pyc b/src/lib/__pycache__/json_to_apkg.cpython-38.pyc index b2aff1d..b9f21e0 100644 Binary files a/src/lib/__pycache__/json_to_apkg.cpython-38.pyc and b/src/lib/__pycache__/json_to_apkg.cpython-38.pyc differ diff --git a/src/lib/helpers.py b/src/lib/helpers.py index eea08fa..1d1dfb6 100644 --- a/src/lib/helpers.py +++ b/src/lib/helpers.py @@ -1,8 +1,10 @@ import os import re +import sys from bs4.element import ResultSet, Tag from kivy import platform +from kivy import user_home_dir from src.lib.strings import get_text @@ -11,27 +13,47 @@ def is_platform(os_name) -> bool: return os_name == platform -def get_root_path() -> str: +# def resource_path(relative_path): +# """ Get the absolute path to the resource, works for dev and for PyInstaller """ +# try: +# # PyInstaller creates a temp folder and stores path in _MEIPASS +# base_path = sys._MEIPASS +# except Exception: +# base_path = os.path.abspath(".") +# base_path = os.path.join(user_home_dir) +# +# return os.path.join(base_path, relative_path) + + +def get_root_path(db=False, media=False) -> str: + print("Home DIR:", user_home_dir) + print(db, media) if is_platform('android'): # if 'ANDROID_STORAGE' in os.environ: from android.storage import app_storage_path # path = f'{app_storage_path()}/' package_name = app_storage_path().split('/')[-2] - path = f'/storage/emulated/0/Android/data/{package_name}/files/' + base_path = f'/storage/emulated/0/Android/data/{package_name}/files/' + if media: + base_path = f'/storage/emulated/0/Android/data/{package_name}/files/media/' + if db: + base_path = f'/storage/emulated/0/{get_text("app_title")}/' else: # platform == 'win' - path = 'files/' - if not os.path.exists(path + 'media/'): - os.makedirs(path + 'media/') - return path - - -def get_db_path() -> str: - if is_platform('android'): # if 'ANDROID_STORAGE' in os.environ: - path = f'/storage/emulated/0/{get_text("app_title")}/' - else: # platform == 'win' - path = 'files/' - if not os.path.exists(path): - os.makedirs(path) - return path + try: + # PyInstaller creates a temp folder and stores path in _MEIPASS + base_path = os.path.join(sys._MEIPASS, get_text("app_title"), 'files') + if media: + base_path = os.path.join(sys._MEIPASS, get_text("app_title"), 'files', 'media') + except Exception: + # base_path = os.path.abspath(".") + base_path = os.path.join(user_home_dir, get_text("app_title"), 'files') + if media: + base_path = os.path.join(user_home_dir, get_text("app_title"), 'files', 'media') + if db: + base_path = os.path.join(user_home_dir, get_text("app_title")) + if not os.path.exists(base_path): + os.makedirs(base_path) + print(base_path) + return base_path class SuspiciousOperation(Exception): diff --git a/src/lib/json_to_apkg.py b/src/lib/json_to_apkg.py index 4a9c309..b08521e 100644 --- a/src/lib/json_to_apkg.py +++ b/src/lib/json_to_apkg.py @@ -1,4 +1,5 @@ import os +import re import random from time import time from datetime import datetime as dt @@ -104,15 +105,15 @@ def generate_apkg(self, deck_tuple, notes) -> str: my_deck.add_note(note) # add media - root_path = get_root_path() + media_path = get_root_path(media=True) my_package = genanki.Package(my_deck) - my_package.media_files = [root_path + 'media/' + file for file in set(media_filenames)] + my_package.media_files = [os.path.join(media_path, file) for file in set(media_filenames)] # generate apkg # my_package.write_to_file('output-' + j_dict["word"] + '.apkg') # apkg_filename = 'output-' + dt.now().strftime("%Y%m%d%H%M%S") + '.apkg' - apkg_filename = 'output' + '.apkg' - # print('before writing') - my_package.write_to_file(root_path + apkg_filename) + root_path = get_root_path() + apkg_filename = 'output.apkg' + my_package.write_to_file(os.path.join(root_path, apkg_filename)) return apkg_filename def generate_note(self, j_dict, tags):