-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathutils.py
executable file
·67 lines (51 loc) · 1.74 KB
/
utils.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
67
import os
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
def get_soup(page_url):
""" Returns BeautifulSoup object of the url provided """
try:
req = requests.get(page_url)
except Exception:
print('Failed to establish a connection with the website')
return
if req.status_code == 404:
print('Page not found')
return
content = req.content
soup = BeautifulSoup(content, 'html.parser')
return soup
def zero_prefix(num):
""" Adds '0' as a prefix to numbers less than 10 """
if num < 10:
return '0' + str(num)
else:
return str(num)
def is_internet_connected():
""" Checks if the computer is connected to the internet """
try:
urlopen('http://216.58.192.142', timeout=1) # '216.58.192.142' is one of the IPs of www.google.com
return True
except Exception:
return False
def path_check(path, folder):
""" Check if the folder for the respective series exists, otherwise create a new folder """
if folder not in os.listdir(path):
os.mkdir(path + folder)
def display(series, season, episode, title):
print(series, 'Season', season + ': Episode', episode, title)
def download_file(file_name, url):
""" Use this function if you want to download the episode from the terminal """
req = requests.get(url)
if req.status_code == 404 or '404' in req.url:
print('No such file found')
return
if file_name in str(req.content):
print('File found')
filename = url.split('/')[-1]
print('Downloading...')
with open(filename, 'wb') as fobj:
fobj.write(req.content)
print('Download complete')
else:
print('error')