diff --git a/robocyp.py b/robocyp.py index 659f982..ada973c 100755 --- a/robocyp.py +++ b/robocyp.py @@ -17,6 +17,7 @@ _playlists = {} DEFAULT_OUTPUT_TMPL = "%(channel)s/%(upload_date)s_%(title)s_[%(id)s].%(ext)s" # download video 1080p or lower with audio DEFAULT_FORMAT = "bestvideo[height<=720]+bestaudio/best[height<=720]" +program_dir = os.path.dirname(os.path.realpath(__file__)) def _truncate_title(title: str, length: int = 30) -> str: @@ -32,14 +33,15 @@ def _truncate_title(title: str, length: int = 30) -> str: def get_yt_creds(): """ Get YouTube API credentials """ creds = None - client_secrets_file = "client_secrets_file.json" + client_secrets_file = os.path.join(program_dir, "client_secrets_file.json") scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"] # The file token.json stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. - if os.path.exists("token.json"): - creds = Credentials.from_authorized_user_file("token.json", scopes) + token_file = os.path.join(program_dir, "token.json") + if os.path.exists(token_file): + creds = Credentials.from_authorized_user_file(token_file, scopes) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: @@ -54,7 +56,7 @@ def get_yt_creds(): ) creds = flow.run_local_server(port=0) # Save the credentials for the next run - with open("token.json", "w") as token: + with open(token_file, "w") as token: token.write(creds.to_json()) return creds @@ -71,10 +73,11 @@ def exit_on_exceeded_quota(e: HttpError): def read_playlists_file(): """ Read playlists.csv and return a dictionary of playlist names to playlist IDs """ global _playlists - if not os.path.exists('playlists.csv'): - print('playlists.csv not found') + playlists_file = os.path.join(program_dir, 'playlists.csv') + if not os.path.exists(playlists_file): + print(f'{playlists_file} not found') return {} - with open('playlists.csv', newline='') as csvfile: + with open(playlists_file, newline='') as csvfile: reader = csv.DictReader(csvfile) _playlists = {row['name']: row['playlist_id'] for row in reader} @@ -352,7 +355,7 @@ def main(): plitems_processed.add(video_id) elif args.command == "download": - db = TinyDB('db.json') + db = TinyDB(os.path.join(program_dir, 'db.json')) query = Query() ydl_opts = { 'outtmpl': os.path.join(args.dst_folder, DEFAULT_OUTPUT_TMPL),