From db63d09c82aca9fe3a5a6136d4ecdfc819e37fa3 Mon Sep 17 00:00:00 2001 From: Richard Mwewa <74001397+rly0nheart@users.noreply.github.com> Date: Sat, 1 Oct 2022 12:52:24 +0200 Subject: [PATCH] Added test_download_video.py, updated README.md and refactored downloader.py --- .../inspectionProfiles/profiles_settings.xml | 6 + .idea/workspace.xml | 39 ++++++ README.md | 1 - downloader.py | 132 +++++++++--------- test_download_video.py | 6 + 5 files changed, 115 insertions(+), 69 deletions(-) create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/workspace.xml create mode 100644 test_download_video.py diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..d133804 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + 1664618714345 + + + + \ No newline at end of file diff --git a/README.md b/README.md index 662e25a..4782620 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,6 @@ python downloader.py https://www.facebook.com/PageName/videos/VideoID |---------|:-----------:| | *-A/--audio* | download audio only (coming soon) | | *-o/--output* | output filename | -| *-d/--debug* | enable debug mode | | *-v/--version* | show program's version number and exit | # Donations diff --git a/downloader.py b/downloader.py index 07af802..6475c74 100644 --- a/downloader.py +++ b/downloader.py @@ -8,94 +8,90 @@ from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys -class FacebookDownloader: - - def __init__(self, args): - self.option = webdriver.FirefoxOptions() - self.option.add_argument('--headless') - self.driver = webdriver.Firefox(options=self.option) - self.program_version_number = "2022.1.0.0" - self.downloading_url = "https://getfvid.com" - self.update_check_endpoint = "https://api.github.com/repos/rly0nheart/Facebook-Downloader/releases/latest" +option = webdriver.FirefoxOptions() +option.add_argument('--headless') +driver = webdriver.Firefox(options=option) +program_version_number = "2022.1.0.0" +downloading_url = "https://getfvid.com" +update_check_endpoint = "https://api.github.com/repos/rly0nheart/Facebook-Downloader/releases/latest" - def notice(self): - notice_msg = f""" - Facebook-Downloader {self.program_version_number} Copyright (C) 2022 Richard Mwewa +def notice(): + notice_msg = f""" +Facebook-Downloader {program_version_number} Copyright (C) 2022 Richard Mwewa - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - """ - return notice_msg +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. +""" + print(notice_msg) - def check_and_get_updates(self): - print(self.notice()) - response = requests.get(self.update_check_endpoint).json() - if response['tag_name'] == self.program_version_number: - """Ignore if the program is up to date""" - pass +def check_and_get_updates(): + notice() + response = requests.get(update_check_endpoint).json() + if response['tag_name'] == program_version_number: + """Ignore if the program is up to date""" + pass + else: + update_prompt = input(f"[?] A new release is available ({response['tag_name']}). Would you like to install it? (y/n) ") + if update_prompt.lower() == "y": + files_to_update = ['downloader.py', 'geckodriver.exe', 'README.md', 'requirements.txt'] + for file in tqdm(files_to_update, desc=f'Updating'): + data = requests.get(f'https://raw.githubusercontent.com/rly0nheart/Facebook-Downloader/master/{file}') + with open(file, "wb") as f: + f.write(data.content) + f.close() + print("Updated: Re-run program.") else: - update_prompt = input(f"[?] A new release is available ({response['tag_name']}). Would you like to install it? (y/n) ") - if update_prompt.lower() == "y": - files_to_update = ['downloader.py', 'geckodriver.exe', 'README.md', 'requirements.txt'] - for file in tqdm(files_to_update, desc=f'Updating'): - data = requests.get(f'https://raw.githubusercontent.com/rly0nheart/Facebook-Downloader/master/{file}') - with open(file, "wb") as f: - f.write(data.content) - f.close() - logging.info(f"Updated: Re-run program.") - else: - pass + pass + - - def download_video(self): - self.check_and_get_updates() - self.driver.get(self.downloading_url) # Opening getfvid.com, a website that downloads facebook videos - url_entry_field = self.driver.find_element(By.NAME, "url") # Find the url entry field - url_entry_field.send_keys(args.url) # write facebook url in the entry field - url_entry_field.send_keys(Keys.ENTER) # press enter - time.sleep(20) # Sleep for at least 20 seconds to wait for the next page to load - self.driver.refresh - - """ - HD: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[1]/a" - SD: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[2]/a" - Audio: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[3]/a" - """ +def download_video(url, output): + driver.get(downloading_url) # Opening getfvid.com, a website that downloads facebook videos + url_entry_field = driver.find_element(By.NAME, "url") # Find the url entry field + url_entry_field.send_keys(url) # write facebook url in the entry field + url_entry_field.send_keys(Keys.ENTER) # press enter + print('Please standby (20 seconds)...') + time.sleep(20) # Sleep for at least 20 seconds to wait for the next page to load + driver.refresh + + """ + HD: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[1]/a" + SD: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[2]/a" + Audio: "/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[3]/a" + """ - download_btn = self.driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[1]/a') # Find the download button (this clicks the first button which returns a video in hd) - download_url = download_btn.get_attribute('href') - - with requests.get(download_url, stream=True) as response: - response.raise_for_status() - with open(f'downloads/{args.output}.mp4', 'wb') as file: - for chunk in tqdm(response.iter_content(chunk_size=8192), desc=f'Downloading: {args.output}.mp4'): - file.write(chunk) - print(f'Downloaded: {file.name}') - self.driver.close() + download_btn = driver.find_element(By.XPATH, '/html/body/div[2]/div/div/div[1]/div/div[2]/div/div[3]/p[1]/a') # Find the download button (this clicks the first button which returns a video in hd) + download_url = download_btn.get_attribute('href') + + with requests.get(download_url, stream=True) as response: + response.raise_for_status() + with open(f'downloads/{output}.mp4', 'wb') as file: + for chunk in tqdm(response.iter_content(chunk_size=8192), desc=f'Downloading: {output}.mp4'): + file.write(chunk) + print(f'Downloaded: {file.name}') + driver.close() parser = argparse.ArgumentParser(description='Facebook-Downloader — by Richard Mwewa') parser.add_argument('url', help='facebook video url (eg. https://www.facebook.com/PageName/videos/VideoID') parser.add_argument('-A', '--audio', help=argparse.SUPPRESS, action='store_true') parser.add_argument('-o', '--output', help='output filename') -parser.add_argument('-d', '--debug', help='enable debug mode', action='store_true') parser.add_argument('-v', '--version', version='2022.1.0.0', action='version') args = parser.parse_args() -if args.debug: - logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%I:%M:%S%p', level=logging.DEBUG) -else: - logging.basicConfig(format='[%(asctime)s] %(message)s', datefmt='%I:%M:%S%p', level=logging.INFO) +url = args.url +output = args.output + if __name__ == "__main__": try: - FacebookDownloader(args).download_video() + check_and_get_updates() + download_video(url, output) except KeyboardInterrupt: - logging.warning('Process interrupted with Ctrl+C.') + print('Process interrupted with Ctrl+C.') except Exception as e: - logging.error(f'An error occured: {e}') \ No newline at end of file + print('An error occured:', e) \ No newline at end of file diff --git a/test_download_video.py b/test_download_video.py new file mode 100644 index 0000000..0764dc0 --- /dev/null +++ b/test_download_video.py @@ -0,0 +1,6 @@ +from downloader import download_video + +def test_download_video(): + # I find this video very interesting, enjoy! ;) + url = 'https://www.facebook.com/VICE/videos/663211078474482' + download_video(url, output='test_video_making-a-weed-smoothie')