@@ -24,11 +24,16 @@ def get_full_time():
24
24
START_TIME_LONG = get_time ()
25
25
26
26
# Get current working directory of spidy
27
- from os import path
27
+ from os import path , makedirs
28
28
29
29
CRAWLER_DIR = path .dirname (path .realpath (__file__ ))
30
30
31
31
# Open log file for logging
32
+ try :
33
+ makedirs ('logs\\ ' ) # Attempts to make the logs directory
34
+ except OSError :
35
+ pass # Assumes only OSError wil complain logs/ already exists
36
+
32
37
LOG_FILE = open ('{0}\\ logs\\ spidy_log_{1}.txt' .format (CRAWLER_DIR , START_TIME ), 'w+' )
33
38
LOG_FILE_NAME = 'logs\\ spidy_log_{0}' .format (START_TIME )
34
39
@@ -49,7 +54,6 @@ def write_log(message):
49
54
import requests
50
55
import shutil
51
56
from lxml import html , etree
52
- from os import makedirs
53
57
from winsound import Beep
54
58
55
59
@@ -486,16 +490,20 @@ def init():
486
490
487
491
# Getting Arguments
488
492
489
- write_log ('[INIT]: Should spidy load settings from an available config file? (y/n):' )
490
- input_ = input ()
491
- if not bool (input_ ):
492
- USE_CONFIG = False
493
- elif input_ in yes :
494
- USE_CONFIG = True
495
- elif input_ in no :
493
+ if not path .exists ('config\\ ' ):
494
+ write_log ('[INFO]: No config folder available.' )
496
495
USE_CONFIG = False
497
496
else :
498
- handle_invalid_input ()
497
+ write_log ('[INIT]: Should spidy load settings from an available config file? (y/n):' )
498
+ input_ = input ()
499
+ if not bool (input_ ):
500
+ USE_CONFIG = False
501
+ elif input_ in yes :
502
+ USE_CONFIG = True
503
+ elif input_ in no :
504
+ USE_CONFIG = False
505
+ else :
506
+ handle_invalid_input ()
499
507
500
508
if USE_CONFIG :
501
509
try :
@@ -677,13 +685,19 @@ def init():
677
685
else :
678
686
write_log ('[INIT]: Loading save files...' )
679
687
# Import saved TODO file data
680
- with open (TODO_FILE , 'r' ) as f :
681
- contents = f .readlines ()
688
+ try :
689
+ with open (TODO_FILE , 'r' ) as f :
690
+ contents = f .readlines ()
691
+ except FileNotFoundError : # If no TODO file is present
692
+ contents = []
682
693
for line in contents :
683
694
TODO .append (line .strip ())
684
695
# Import saved done file data
685
- with open (DONE_FILE , 'r' ) as f :
686
- contents = f .readlines ()
696
+ try :
697
+ with open (DONE_FILE , 'r' ) as f :
698
+ contents = f .readlines ()
699
+ except FileNotFoundError : # If no DONE file is present
700
+ contents = []
687
701
for line in contents :
688
702
DONE .append (line .strip ())
689
703
del contents
@@ -695,10 +709,8 @@ def init():
695
709
696
710
def main ():
697
711
"""
698
- The main function or spidy.
712
+ The main function of spidy.
699
713
"""
700
- init ()
701
-
702
714
# Declare global variables
703
715
global VERSION , START_TIME , START_TIME_LONG
704
716
global LOG_FILE , LOG_FILE_NAME , ERR_LOG_FILE_NAME
@@ -709,6 +721,21 @@ def main():
709
721
global TODO_FILE , DONE_FILE , ERR_LOG_FILE , WORD_FILE , BAD_FILE
710
722
global WORDS , TODO , DONE
711
723
724
+ init ()
725
+
726
+ # Create required saved/ folder
727
+ try :
728
+ makedirs ('saved\\ ' )
729
+ except OSError :
730
+ pass # Assumes only OSError wil complain saved/ already exists
731
+
732
+ # Create required files
733
+ with open (WORD_FILE , 'w' ):
734
+ pass
735
+
736
+ with open (BAD_FILE , 'w' ):
737
+ pass
738
+
712
739
write_log ('[INIT]: Successfully started spidy Web Crawler version {0}...' .format (VERSION ))
713
740
log ('LOG: Successfully started crawler.' )
714
741
0 commit comments