Skip to content

Commit 16b5739

Browse files
committed
Handle Errors
1 parent 3c0d6fb commit 16b5739

File tree

3 files changed

+70
-25
lines changed

3 files changed

+70
-25
lines changed

helpers/db_helper.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,36 @@ def create_database_session(
2929

3030

3131
def run_revisions(alembic_directory: str, database_path: str = ""):
32-
ini_path = os.path.join(alembic_directory, "alembic.ini")
33-
script_location = os.path.join(alembic_directory, "alembic")
34-
full_database_path = f"sqlite:///{database_path}"
35-
alembic_cfg = Config(ini_path)
36-
alembic_cfg.set_main_option("script_location", script_location)
37-
alembic_cfg.set_main_option("sqlalchemy.url", full_database_path)
38-
command.upgrade(alembic_cfg, "head")
39-
command.revision(alembic_cfg, autogenerate=True, message="content")
32+
while True:
33+
try:
34+
ini_path = os.path.join(alembic_directory, "alembic.ini")
35+
script_location = os.path.join(alembic_directory, "alembic")
36+
full_database_path = f"sqlite:///{database_path}"
37+
alembic_cfg = Config(ini_path)
38+
alembic_cfg.set_main_option("script_location", script_location)
39+
alembic_cfg.set_main_option("sqlalchemy.url", full_database_path)
40+
command.upgrade(alembic_cfg, "head")
41+
command.revision(alembic_cfg, autogenerate=True, message="content")
42+
break
43+
except Exception as e:
44+
print(e)
45+
print
4046

4147

4248
def run_migrations(alembic_directory: str, database_path: str) -> None:
43-
ini_path = os.path.join(alembic_directory, "alembic.ini")
44-
script_location = os.path.join(alembic_directory, "alembic")
45-
full_database_path = f"sqlite:///{database_path}"
46-
alembic_cfg = Config(ini_path)
47-
alembic_cfg.set_main_option("script_location", script_location)
48-
alembic_cfg.set_main_option("sqlalchemy.url", full_database_path)
49-
command.upgrade(alembic_cfg, "head")
49+
while True:
50+
try:
51+
ini_path = os.path.join(alembic_directory, "alembic.ini")
52+
script_location = os.path.join(alembic_directory, "alembic")
53+
full_database_path = f"sqlite:///{database_path}"
54+
alembic_cfg = Config(ini_path)
55+
alembic_cfg.set_main_option("script_location", script_location)
56+
alembic_cfg.set_main_option("sqlalchemy.url", full_database_path)
57+
command.upgrade(alembic_cfg, "head")
58+
break
59+
except Exception as e:
60+
print(e)
61+
print
5062

5163

5264
class database_collection(object):
@@ -63,14 +75,13 @@ def database_picker(self, database_name):
6375
return database
6476

6577

66-
6778
def create_auth_array(item):
6879
auth_array = item.__dict__
6980
auth_array["support_2fa"] = False
7081
return auth_array
7182

7283

73-
def get_or_create(session: Session, model, defaults=None, fbkwargs:dict={}):
84+
def get_or_create(session: Session, model, defaults=None, fbkwargs: dict = {}):
7485
fbkwargs2 = fbkwargs.copy()
7586
instance = session.query(model).filter_by(**fbkwargs2).one_or_none()
7687
if instance:

helpers/main_helper.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ async def format_image(filepath: str, timestamp: float):
136136
break
137137

138138

139-
async def async_downloads(download_list: list[template_media_table], subscription: create_user):
139+
async def async_downloads(
140+
download_list: list[template_media_table], subscription: create_user
141+
):
140142
async def run(download_list: list[template_media_table]):
141143
session_m = subscription.session_manager
142144
proxies = session_m.proxies
@@ -166,7 +168,9 @@ async def run(download_list: list[template_media_table]):
166168
responses = await asyncio.gather(*tasks)
167169
tasks.clear()
168170

169-
async def check(download_item: template_media_table, response: ClientResponse):
171+
async def check(
172+
download_item: template_media_table, response: ClientResponse
173+
):
170174
filepath = os.path.join(download_item.directory, download_item.filename)
171175
response_status = False
172176
if response.status == 200:
@@ -487,16 +491,21 @@ def export_sqlite2(archive_path, datas, parent_type, legacy_fixer=False):
487491

488492

489493
def legacy_sqlite_updater(
490-
legacy_metadata_path:str, api_type:str, subscription:create_user, delete_metadatas:list
494+
legacy_metadata_path: str,
495+
api_type: str,
496+
subscription: create_user,
497+
delete_metadatas: list,
491498
):
492499
final_result = []
493500
if os.path.exists(legacy_metadata_path):
494501
cwd = os.getcwd()
495-
alembic_location = os.path.join(cwd, "database", "archived_databases", api_type.lower())
502+
alembic_location = os.path.join(
503+
cwd, "database", "archived_databases", api_type.lower()
504+
)
496505
db_helper.run_migrations(alembic_location, legacy_metadata_path)
497506
database_name = "user_data"
498507
session, engine = db_helper.create_database_session(legacy_metadata_path)
499-
database_session:Session = session()
508+
database_session: Session = session()
500509
db_collection = db_helper.database_collection()
501510
database = db_collection.database_picker(database_name)
502511
if database:
@@ -526,10 +535,10 @@ def legacy_sqlite_updater(
526535
return final_result, delete_metadatas
527536

528537

529-
def export_sqlite(database_path:str, api_type, datas):
538+
def export_sqlite(database_path: str, api_type, datas):
530539
metadata_directory = os.path.dirname(database_path)
531540
os.makedirs(metadata_directory, exist_ok=True)
532-
database_name = os.path.basename(database_path).replace(".db","")
541+
database_name = os.path.basename(database_path).replace(".db", "")
533542
cwd = os.getcwd()
534543
alembic_location = os.path.join(cwd, "database", "databases", database_name.lower())
535544
db_helper.run_migrations(alembic_location, database_path)
@@ -1171,3 +1180,21 @@ def module_chooser(domain, json_sites):
11711180
string = f"{domain} not supported"
11721181
site_names = []
11731182
return string, site_names
1183+
1184+
1185+
async def move_to_old(
1186+
folder_directory: str,
1187+
base_download_directories: list,
1188+
first_letter: str,
1189+
model_username: str,
1190+
source: str,
1191+
):
1192+
# MOVE TO OLD
1193+
local_destinations = [
1194+
os.path.join(x, folder_directory) for x in base_download_directories
1195+
]
1196+
local_destination = check_space(local_destinations, min_size=100)
1197+
local_destination = os.path.join(local_destination, first_letter, model_username)
1198+
print(f"Moving {source} -> {local_destination}")
1199+
shutil.copytree(source, local_destination, dirs_exist_ok=True)
1200+
shutil.rmtree(source)

modules/onlyfans.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
from typing import Any, Optional, Union
1111
from urllib.parse import urlparse
1212

13+
from sqlalchemy.exc import OperationalError
14+
1315
import extras.OFLogin.start_ofl as oflogin
1416
import extras.OFRenamer.start_ofr as ofrenamer
1517
import helpers.db_helper as db_helper
@@ -1244,7 +1246,12 @@ async def prepare_downloads(subscription: create_user):
12441246
if media_set_count:
12451247
print(string)
12461248
await main_helper.async_downloads(download_list, subscription)
1247-
database_session.commit()
1249+
while True:
1250+
try:
1251+
database_session.commit()
1252+
break
1253+
except OperationalError:
1254+
database_session.rollback()
12481255
database_session.close()
12491256
print
12501257
print

0 commit comments

Comments
 (0)