@@ -42,6 +42,44 @@ def print_info():
4242 print (help_text .strip ())
4343
4444
45+ def database_make_connection ():
46+ """
47+ Get a connection to the database.
48+
49+ Returns:
50+ (sqlite3.Connection): An open SQLite database connection.
51+
52+ """
53+ return sqlite3 .connect (os .path .dirname (os .path .abspath (__file__ )) + '/tombll.db' )
54+
55+
56+ def database_begin_write (con ):
57+ """
58+ Run this before you call commit() when you write data as a starting point.
59+
60+ If there is an data base error it will role back and exit the program.
61+ If it comes back sane you should run con.commit()
62+
63+ Args:
64+ con (sqlite3.Connection): An open SQLite database connection.
65+
66+ """
67+ cur = con .cursor ()
68+ cur .execute ("BEGIN;" )
69+
70+
71+ def database_commit_and_close (con ):
72+ """
73+ Commit and close in one line or just run close if you only read data.
74+
75+ Args:
76+ con (sqlite3.Connection): An open SQLite database connection.
77+
78+ """
79+ con .commit ()
80+ con .close ()
81+
82+
4583def print_list (con ):
4684 """
4785 Retrieve and prints a list of level information.
@@ -61,6 +99,16 @@ def print_list(con):
6199 print (row )
62100
63101
102+ def trle_level_url (lid ):
103+ """
104+ Get level TRLE url from lid number.
105+
106+ Args:
107+ con (sqlite3.Connection): An open SQLite database connection.
108+ """
109+ return f"https://www.trle.net/sc/levelfeatures.php?lid={ lid } "
110+
111+
64112def print_download_list (level_id , con ):
65113 """
66114 Print a list of ZIP file entries associated with a specific level.
@@ -305,110 +353,144 @@ def update_tombll_json_to_database(data, level_id, con):
305353if __name__ == "__main__" :
306354 number_of_argument = len (sys .argv )
307355 if (number_of_argument == 1 or number_of_argument >= 10 ):
308- print ("here" )
309356 print_info ()
310357 sys .exit (1 )
358+
311359 if (sys .argv [1 ] == "-h" and number_of_argument == 2 ):
312360 print_info ()
313361 sys .exit (1 )
314362
315- main_con = sqlite3 .connect (os .path .dirname (os .path .abspath (__file__ )) + '/tombll.db' )
316363 if (sys .argv [1 ] == "-l" and number_of_argument == 2 ):
364+ main_con = database_make_connection ()
317365 print_list (main_con )
366+ main_con .close ()
367+
318368 elif (sys .argv [1 ] == "-a" and number_of_argument == 3 ):
319- main_cur = main_con .cursor ()
320- main_cur .execute ("BEGIN;" )
321369 main_lid = sys .argv [2 ]
322370 main_data = data_factory .make_trle_tombll_data ()
323- main_soup = scrape_trle . scrape_common . get_soup (
324- f"https://www.trle.net/sc/levelfeatures.php?lid= { main_lid } " )
371+
372+ main_soup = scrape_trle . scrape_common . get_soup ( trle_level_url ( main_lid ) )
325373 scrape_trle .get_trle_level (main_soup , main_data )
326- add_tombll_json_to_database (main_data , main_con )
327- main_con .commit ()
328- print (f"lid { sys .argv [2 ]} added successfully." )
374+
375+ if main_data ['title' ]:
376+ main_con = database_make_connection ()
377+ database_begin_write (main_con )
378+ add_tombll_json_to_database (main_data , main_con )
379+ database_commit_and_close (main_con )
380+ print (f"lid { main_lid } with title \" { main_data ['title' ]} \" added successfully." )
381+ else :
382+ print (f"lid { main_lid } was an empty page." )
329383
330384 elif (sys .argv [1 ] == "-aj" and number_of_argument == 4 ):
331385 main_lid = sys .argv [2 ]
386+ main_path = sys .argv [3 ]
332387 main_data = data_factory .make_trle_tombll_data ()
333- main_soup = scrape_trle . scrape_common . get_soup (
334- f"https://www.trle.net/sc/levelfeatures.php?lid= { main_lid } " )
388+
389+ main_soup = scrape_trle . scrape_common . get_soup ( trle_level_url ( main_lid ) )
335390 scrape_trle .get_trle_level (main_soup , main_data )
336- with open (sys .argv [3 ], mode = 'w' , encoding = 'utf-8' ) as json_file :
337- json .dump (main_data , json_file )
338- print (f"Level { sys .argv [2 ]} saved to { sys .argv [3 ]} successfully." )
391+
392+ if main_data ['title' ]:
393+ with open (main_path , mode = 'w' , encoding = 'utf-8' ) as json_file :
394+ json .dump (main_data , json_file )
395+ print (
396+ f"lid { main_lid } with title \" { main_data ['title' ]} \" "
397+ f"saved to { main_path } successfully."
398+ )
399+ else :
400+ print (f"lid { main_lid } was an empty page." )
339401
340402 elif (sys .argv [1 ] == "-af" and number_of_argument == 3 ):
341- main_data = tombll_common .get_tombll_json (sys .argv [2 ])
342- main_cur = main_con .cursor ()
343- main_cur .execute ("BEGIN;" )
403+ main_path = sys .argv [2 ]
404+ main_data = tombll_common .get_tombll_json (main_path )
405+
406+ main_con = database_make_connection ()
407+ database_begin_write (main_con )
344408 add_tombll_json_to_database (main_data , main_con )
345- main_con . commit ( )
346- print (f"File { sys . argv [ 2 ] } added successfully." )
409+ database_commit_and_close ( main_con )
410+ print (f"File { main_path } with title \" { main_data [ 'title' ] } \" added successfully." )
347411
348412 elif (sys .argv [1 ] == "-ac" and number_of_argument == 3 ):
349- main_cur = main_con .cursor ()
350- main_cur .execute ("BEGIN;" )
351413 main_lid = sys .argv [2 ]
352414 main_data = data_factory .make_trle_tombll_data ()
353- main_soup = scrape_trle . scrape_common . get_soup (
354- f"https://www.trle.net/sc/levelfeatures.php?lid= { main_lid } " )
415+
416+ main_soup = scrape_trle . scrape_common . get_soup ( trle_level_url ( main_lid ) )
355417 scrape_trle .get_trle_level_card (main_soup , main_data )
356- add_tombll_json_to_database (main_data , main_con )
357- main_con .commit ()
358- print (f"lid { sys .argv [2 ]} card added successfully." )
418+
419+ if main_data ['title' ]:
420+ main_con = database_make_connection ()
421+ database_begin_write (main_con )
422+ add_tombll_json_to_database (main_data , main_con )
423+ database_commit_and_close (main_con )
424+ print (f"lid { main_lid } card with title \" { main_data ['title' ]} \" added successfully." )
425+ else :
426+ print (f"lid { main_lid } was an empty page." )
359427
360428 elif (sys .argv [1 ] == "-acr" and number_of_argument == 4 ):
361- main_cur = main_con .cursor ()
362- main_cur .execute ("BEGIN;" )
363429 main_range_a = int (sys .argv [2 ])
364430 main_range_b = int (sys .argv [3 ])
365431
366432 for i in range (min (main_range_a , main_range_b ), max (main_range_a , main_range_b ) + 1 ):
367- print (i )
368433 main_data = data_factory .make_trle_tombll_data ()
369- main_soup = scrape_trle .scrape_common .get_soup (
370- f"https://www.trle.net/sc/levelfeatures.php?lid={ i } " )
434+ main_soup = scrape_trle .scrape_common .get_soup (trle_level_url (i ))
371435 scrape_trle .get_trle_level_card (main_soup , main_data )
372- add_tombll_json_to_database (main_data , main_con )
373- main_con .commit ()
374- print (f"lid { i } card added successfully." )
436+
437+ if main_data ['title' ]:
438+ main_con = database_make_connection ()
439+ database_begin_write (main_con )
440+ add_tombll_json_to_database (main_data , main_con )
441+ database_commit_and_close (main_con )
442+ print (f"lid { i } card with title \" { main_data ['title' ]} \" added successfully." )
443+ else :
444+ print (f"lid { i } was an empty page." )
445+
375446 time .sleep (5 )
376447
377448 elif (sys .argv [1 ] == "-rm" and number_of_argument == 3 ):
378- main_level_id = tombll_read .database_level_id (sys .argv [2 ], main_con )
379- main_cur = main_con .cursor ()
380- main_cur .execute ("BEGIN;" )
381- tombll_delete .database_level (main_level_id , main_con )
382- main_con .commit ()
383- print (f"Level { sys .argv [2 ]} removed successfully." )
449+ main_lid = sys .argv [2 ]
450+ main_con = database_make_connection ()
451+ main_database_level_id = tombll_read .database_level_id (main_lid , main_con )
452+
453+ database_begin_write (main_con )
454+ tombll_delete .database_level (main_database_level_id , main_con )
455+ database_commit_and_close (main_con )
456+ print (f"lid { main_lid } removed successfully." )
384457
385458 elif (sys .argv [1 ] == "-u" and number_of_argument == 3 ):
386- main_cur = main_con .cursor ()
387- main_cur .execute ("BEGIN;" )
388459 main_lid = sys .argv [2 ]
389460 main_data = data_factory .make_trle_tombll_data ()
390- main_soup = scrape_trle . scrape_common . get_soup (
391- f"https://www.trle.net/sc/levelfeatures.php?lid= { main_lid } " )
461+
462+ main_soup = scrape_trle . scrape_common . get_soup ( trle_level_url ( main_lid ) )
392463 scrape_trle .get_trle_level (main_soup , main_data )
393- main_level_id = tombll_read .database_level_id (main_lid , main_con )
394- update_tombll_json_to_database (main_data , main_level_id , main_con )
395- main_con .commit ()
396- print (f"Level { sys .argv [2 ]} updated successfully." )
464+
465+ if main_data ['title' ]:
466+ main_con = database_make_connection ()
467+ main_level_id = tombll_read .database_level_id (main_lid , main_con )
468+
469+ database_begin_write (main_con )
470+ update_tombll_json_to_database (main_data , main_level_id , main_con )
471+ database_commit_and_close (main_con )
472+ print (f"lid { main_lid } with title \" { main_data ['title' ]} \" updated successfully." )
473+ else :
474+ print (f"lid { main_lid } was an empty page." )
397475
398476 elif (sys .argv [1 ] == "-lz" and number_of_argument == 3 ):
477+ main_con = database_make_connection ()
399478 print_download_list (sys .argv [2 ], main_con )
479+ main_con .close ()
400480
401481 elif (sys .argv [1 ] == "-az" and number_of_argument >= 5 ):
402482 main_data = data_factory .make_zip_file ()
403483 main_data ["name" ] = sys .argv [3 ]
404484 main_data ["size" ] = sys .argv [4 ]
405485 main_data ["md5" ] = sys .argv [5 ]
406- main_data ["url" ] = sys .argv [6 ] if number_of_argument == 7 else None
407- main_data ["release" ] = sys .argv [7 ] if number_of_argument == 8 else None
408- main_data ["version" ] = sys .argv [8 ] if number_of_argument == 9 else None
486+ main_data ["url" ] = sys .argv [6 ] if number_of_argument >= 7 else None
487+ main_data ["release" ] = sys .argv [7 ] if number_of_argument >= 8 else None
488+ main_data ["version" ] = sys .argv [8 ] if number_of_argument >= 9 else None
489+
490+ main_con = database_make_connection ()
491+ database_begin_write (main_con )
409492 tombll_create .database_zip_file (main_data , sys .argv [2 ], main_con )
410- main_con . commit ( )
493+ database_commit_and_close ( main_con )
411494
412495 else :
413496 print_info ()
414- main_con .close ()
0 commit comments