-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapd.py
78 lines (62 loc) · 2.51 KB
/
apd.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
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
import io
import argparse
import requests as req
from bs4 import BeautifulSoup as bs
import progressbar
#TODO download file option
#TODO rename file after download from output file
def usage():
return """
Use "" around link to avoid bad things :>
adp.py "Link" [quality]
python apd.py "https://www.aparat.com/v/d13rh3"
python apd.py "https://www.aparat.com/v/d13rh3" -q 480
The default quality is 480.
"""
def main():
print("Please wait...")
mainPage = req.get(link).content
main_soup = bs(mainPage, 'html.parser')
main_name = main_soup.find("span", attrs={"class":"d-in v-m"}).text.encode()
playlist = main_soup.find('div', attrs={'class':'playlist-body'})
playListLinks = playlist.find_all('a', attrs={'class':'title'})
video_pages = [f"https://www.aparat.com{video.get('href')}" for video in playListLinks]
count=len(video_pages)
print(f"This playlist contains {count} videos")
bar=progressbar.ProgressBar(maxval=count,
widgets=[progressbar.Bar('=','[',']'),progressbar.Percentage()])
bar.start()
links = {}
for index,page in enumerate(video_pages) :
bar.update(index+1)
html = req.get(page).content
soup = bs(html, 'html.parser')
name = soup.find("h1", attrs={"id":"videoTitle", "class":"title"}).text.encode()
qualitys = soup.find('div', attrs={'class':'dropdown-content'}).find_all('a')
# TODO change this method
for qual in qualitys :
if quality in qual.get('aria-label'):
links[name] = qual.get('href')
elif "480" in qual.get('aria-label'):
links[name] = qual.get('href')
bar.finish()
print("writing download list ...")
with io.open('apd_output.sh', "a", encoding="utf-8") as file:
file.write(f"#!/bin/bash\n")
i = 1
for name, videoLink in links.items():
file.write('aria2c -x16 -s16 -k1M {} -o \"{:03d}_{}.mp4\"\n'.format(videoLink,i,name.decode('utf-8')))
i += 1
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Aparat Playlist Downloader(APD)", usage=usage())
parser.add_argument("link", help="main page Link")
parser.add_argument("-q", "--quality", help="eg: [124, 360, 480, 720, ...]", default='480')
'''parser.add_argument("--debug", action="store_true")'''
args = parser.parse_args()
link = args.link
quality = args.quality
if args.quality :
main()
else:
usage()