-
Notifications
You must be signed in to change notification settings - Fork 48
/
facebook-dl.py
64 lines (50 loc) · 2.18 KB
/
facebook-dl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
#
# pylint: disable=missing-docstring,trailing-whitespace,invalid-name
#
# By Siddharth Dushantha (sdushantha) 2020
#
# Notice: If you are on Windows, the output of this script might
# look a little messy and that is because command prompt
# does not support escape sequences.
#
import re
import sys
import argparse
import requests
# This magic spell lets me erase the current line.
# I can use this to show for example "Downloading..."
# and then "Downloaded" on the line where
# "Downloading..." was.
ERASE_LINE = '\e[2K'
def main():
parser = argparse.ArgumentParser(description="Download videos from facebook from your terminal")
parser.add_argument('url', action="store")
parser.add_argument('resolution', action="store", nargs="?")
args = parser.parse_args()
print("Fetching source code...", end="\r", flush=True)
request = requests.get(args.url)
print(ERASE_LINE, end="\r", flush=True)
print("\033[92m✔\033[0m Fetched source code")
# Create the file name by extracting the video ID from html and then add
# "hd" or "sd" depending on the quality of the resolution that is being downloaded.
#
# To decide whether to use "hd or "sd" we are using an if-then-else
# one-liner statement. It might look a little confusing at first, but it
# makes a lot of sense once you get an understanding of it.
# Read this if you are a little confused: https://stackoverflow.com/a/2802748/9215267
file_name = str(re.findall(r"videos\/(.+?)\"", request.text)[-1].replace("/", "")) + f"_{'sd' if args.resolution == 'sd' else 'hd'}.mp4"
print("Downloading video...", end="\r", flush=True)
try:
request = requests.get(re.findall(f"{'sd_src' if args.resolution == 'sd' else 'hd_src'}:\"(.+?)\"", request.text)[0])
except IndexError:
print(ERASE_LINE, end="\r", flush=True)
print("\e[91m✘\e[0m Video could not be downloaded")
sys.exit()
# Write the content to the file
with open(file_name, "wb") as f:
f.write(request.content)
print(ERASE_LINE, end="\r", flush=True)
print(f"\033[92m✔\033[0m Video downloaded: {file_name}")
if __name__ == "__main__":
main()