-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbdl.py
66 lines (49 loc) · 1.56 KB
/
fbdl.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
65
66
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -*- Coded By VanGans -*-
# -*- SadCode Official -*-
from time import sleep
import re
import sys
import requests
import urllib.request
import random
import argparse
good = "\033[92m✔\033[0m"
# 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 = '\x1b[2K'
# This extracts the video url
def extract_url(html, quality):
if quality == "sd":
# Standard Definition video
url = re.search('sd_src:"(.+?)"', html)[0]
else:
# High Definition video
url = re.search('hd_src:"(.+?)"', html)[0]
# cleaning the url
url = url.replace('hd_src:"', '')
url = url.replace('sd_src:"', '')
url = url.replace('"', "")
return url
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)
r = requests.get(args.url)
sys.stdout.write(ERASE_LINE)
print(good, "Fetched source code")
file_url = extract_url(r.text, args.resolution)
# Generates a random number with will be the file name
path = str(random.random())[3:12] + ".mp4"
print("Downloading video...", end="\r", flush=True)
# Downloads the video
urllib.request.urlretrieve(file_url, path)
sys.stdout.write(ERASE_LINE)
print(good, "Video downloaded:", path)
if __name__=="__main__":
main()