Skip to content

Commit

Permalink
Fixed lxml missing from dependencies, Improved get_root_path in helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmujtabaraza committed Apr 11, 2022
1 parent ff93f45 commit 8aca9cd
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
3 changes: 2 additions & 1 deletion environment.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: vocab-to-anki-app
name: vocab-to-anki
channels:
- conda-forge
dependencies:
Expand All @@ -13,4 +13,5 @@ dependencies:
- ibm-watson
- kivy[full]
- kivymd
- lxml
- user_agent
12 changes: 8 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
# <paths xmlns:android="http://schemas.android.com/apk/res/android">
# <external-path name="external_files" path="Android/data/org.test.vocabtoanki/files" />
# </paths>

# 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

Expand All @@ -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.')


Expand Down
Binary file removed requirements.txt
Binary file not shown.
Binary file modified src/__pycache__/app.cpython-38.pyc
Binary file not shown.
12 changes: 5 additions & 7 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 = """
Expand Down
8 changes: 4 additions & 4 deletions src/dict_scraper/spiders/cambridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -357,16 +357,16 @@ 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:
try:
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})
Expand Down
Binary file modified src/lib/__pycache__/json_to_apkg.cpython-38.pyc
Binary file not shown.
54 changes: 38 additions & 16 deletions src/lib/helpers.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand Down
11 changes: 6 additions & 5 deletions src/lib/json_to_apkg.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import random
from time import time
from datetime import datetime as dt
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 8aca9cd

Please sign in to comment.