Advertisement
Guest User

Untitled

a guest
Oct 30th, 2017
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 36.55 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4.  
  5. import os
  6. import time
  7. import udemy
  8. import optparse
  9.  
  10. from sys import *
  11. from requests import get
  12. from pprint import pprint
  13. from udemy.colorized import *
  14. from udemy import __author__
  15. from udemy import __version__
  16.  
  17. from udemy.colorized.banner import banner
  18.  
  19. extract_info = udemy.UdemyInfoExtractor()
  20. course_dl = udemy.Downloader()
  21. getpass = udemy.GetPass()
  22. cache_creds = udemy.cache_credentials
  23. use_cached_creds = udemy.use_cached_credentials
  24.  
  25. class UdemyDownload:
  26.  
  27. def __init__(self, url, username, password, list_down=False, save_links=False, outto=None, quality=None):
  28. self.url = url
  29. self.username = username
  30. self.password = password
  31. self.list = list_down
  32. self.save = save_links
  33. self.outto = outto
  34. self.quality = quality
  35.  
  36.  
  37.  
  38. def login(self):
  39. extract_info.login(self.username, self.password)
  40.  
  41. def logout(self):
  42. extract_info.logout()
  43.  
  44. def Progress(self, iteration, total, prefix = '' , fileSize='' , downloaded = '' , rate = '' ,suffix = '', barLength = 100):
  45. filledLength = int(round(barLength * iteration / float(total)))
  46. percents = format(100.00 * (iteration / float(total)), '.2f')
  47. bar = fc + sd + ('█' if os.name == 'posix' else '#') * filledLength + fg + sd +'-' * (barLength - filledLength)
  48. stdout.write('{}{}[{}{}*{}{}] : {}{}{}/{} {}% |{}{}{}| {} {}s ETA \r'.format(fc,sd,fm,sb,fc,sd,fg,sb,fileSize,downloaded,percents,bar,fg,sb,rate,suffix))
  49. stdout.flush()
  50.  
  51. def clean_dict(self, udemy_dict):
  52. if not isinstance(udemy_dict, (dict, list)):
  53. return udemy_dict
  54. if isinstance(udemy_dict, list):
  55. return [v for v in (self.clean_dict(v) for v in udemy_dict) if v]
  56. return {k: v for k, v in ((k, self.clean_dict(v)) for k, v in udemy_dict.items()) if v}
  57.  
  58. def Download(self, total, recvd, ratio, rate, eta):
  59. if total <= 1048576:
  60. TotalSize = round(float(total) / 1024, 2)
  61. Receiving = round(float(recvd) / 1024, 2)
  62. Size = format(TotalSize if TotalSize < 1024.00 else TotalSize/1024.00, '.2f')
  63. Received = format(Receiving if Receiving < 1024.00 else Receiving/1024.00,'.2f')
  64. SGb_SMb = 'KB' if TotalSize < 1024.00 else 'MB'
  65. RGb_RMb = 'KB ' if Receiving < 1024.00 else 'MB '
  66. else:
  67. TotalSize = round(float(total) / 1048576, 2)
  68. Receiving = round(float(recvd) / 1048576, 2)
  69. Size = format(TotalSize if TotalSize < 1024.00 else TotalSize/1024.00, '.2f')
  70. Received = format(Receiving if Receiving < 1024.00 else Receiving/1024.00,'.2f')
  71. SGb_SMb = 'MB' if TotalSize < 1024.00 else 'GB'
  72. RGb_RMb = 'MB ' if Receiving < 1024.00 else 'GB '
  73.  
  74. Dl_Speed = round(float(rate) , 2)
  75. dls = format(Dl_Speed if Dl_Speed < 1024.00 else Dl_Speed/1024.00, '.2f')
  76. Mb_kB = 'kB/s ' if Dl_Speed < 1024.00 else 'MB/s '
  77. (mins, secs) = divmod(eta, 60)
  78. (hours, mins) = divmod(mins, 60)
  79. if hours > 99:
  80. eta = "--:--:--"
  81. if hours == 0:
  82. eta = "%02d:%02d" % (mins, secs)
  83. else:
  84. eta = "%02d:%02d:%02d" % (hours, mins, secs)
  85. self.Progress(Receiving, TotalSize, fileSize = str(Size) + str(SGb_SMb) , downloaded = str(Received) + str(RGb_RMb), rate = str(dls) + str(Mb_kB), suffix = str(eta), barLength = 40)
  86.  
  87. def Downloader(self, url, title, path):
  88. out = course_dl.download(url, title, filepath=path, quiet=True, callback=self.Download)
  89. if out == 'EXISTS':
  90. return ('already_exist')
  91. elif out == 401:
  92. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Udemy Says (HTTP Error 401 : Unauthorized)")
  93. print (fc + sd + "[" + fw + sb + "*" + fc + sd + "] : " + fw + sd + "Try to run the udemy-dl again...")
  94. exit(0)
  95.  
  96. def SaveLinks(self, quality=None, path=None, default=False):
  97. if not path:
  98. current_dir = os.getcwd()
  99. else:
  100. if os.path.exists(path):
  101. current_dir = path
  102. os.chdir(current_dir)
  103. else:
  104. current_dir = os.getcwd()
  105. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Path '%s' does not exist, saving to '%s'" % (path, current_dir))
  106.  
  107. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading webpage..")
  108. time.sleep(2)
  109. course_path = extract_info.match_id(self.url)
  110. course = "%s" % (course_path)
  111. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Extracting course information..")
  112. time.sleep(2)
  113. course_name = current_dir + '\\' + course_path if os.name is 'nt' else current_dir + '/' + course_path
  114. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sb + "Downloading " + fb + sb + "'%s'." % (course.replace('-',' ')))
  115. self.login()
  116. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading course information webpages ..")
  117. videos_dict = self.clean_dict(extract_info.real_extract(self.url, course_name, course_path))
  118. self.logout()
  119. if isinstance(videos_dict, dict):
  120. if os.name == 'nt':
  121. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Saving links under '%s\\%s.txt'" % (current_dir, course))
  122. else:
  123. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Saving links under '%s/%s.txt'" % (current_dir, course))
  124. for chap in sorted(videos_dict):
  125. for lecture_name,urls in sorted(videos_dict[chap].items()):
  126. try:
  127. _file = urls.get('file' )
  128. _external_url = urls.get('external_url')
  129. _subtitle = urls.get('subtitle')
  130. except AttributeError as e:
  131. pass
  132. else:
  133. if _external_url and not _file and not _subtitle:
  134. _url = _external_url
  135. elif _file and not _external_url and not _subtitle:
  136. _url = _file
  137. elif _subtitle and not _file and not _external_url:
  138. _url = _subtitle
  139. elif not _external_url and not _file:
  140. if not quality:
  141. _url = max(urls, key=urls.get)
  142. else:
  143. found = False
  144. for url,res in urls.items():
  145. if res == quality:
  146. _url = url
  147. found = True
  148. else:
  149. continue
  150.  
  151. if not found:
  152. continue
  153. if default:
  154. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Request quality not found for (%s)" % (lecture_name))
  155. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fw + sb + "Downloading default quality...")
  156. try:
  157. _url = max(urls, key=urls.get)
  158. except ValueError as e:
  159. continue
  160. else:
  161. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Requested quality is not available for (%s)" % (lecture_name))
  162. if version_info[:2] >= (3, 0):
  163. askUser = input(fc + sd + "[" + fw + sb + "?" + fc + sd + "] : " + fw + sb + "Would you like to download the default quality (y/n): ")
  164. else:
  165. askUser = raw_input(fc + sd + "[" + fw + sb + "?" + fc + sd + "] : " + fw + sb + "Would you like to download the default quality (y/n): ")
  166. if askUser == 'y' or askUser == 'Y' or askUser == '':
  167. try:
  168. _url = max(urls, key=urls.get)
  169. except ValueError as e:
  170. continue
  171. else:
  172. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Continuing to check for the next lecture..")
  173. continue
  174.  
  175. if _url:
  176. with open("%s.txt" % (course), "a") as f:
  177. f.write("[+] -- name : %s\n[+] -- link : %s\n" % (lecture_name, _url))
  178. f.close()
  179. print (fc + sd + "[" + fm + sb + "+" + fc + sd + "] : " + fg + sd + "Saved successfully.")
  180.  
  181.  
  182. def get_filsize(self, url):
  183. req = get(url, stream=True,verify=False)
  184. content_length = float(req.headers.get('Content-Length'))
  185. if content_length <= 1048576.00:
  186. size = round(content_length / 1024, 2)
  187. sz = size
  188. in_MB = "KB "
  189. else:
  190. size = round(content_length / 1048576, 2)
  191. sz = size if size < 1024.00 else round(size/1024.00,2)
  192. in_MB = "MB " if size < 1024.00 else 'GB '
  193.  
  194. return sz,in_MB
  195.  
  196. def ListDown(self):
  197. current_dir = os.getcwd()
  198. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading webpage..")
  199. time.sleep(2)
  200. course_path = extract_info.match_id(self.url)
  201. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Extracting course information..")
  202. time.sleep(2)
  203. course_name = current_dir + '\\' + course_path if os.name is 'nt' else current_dir + '/' + course_path
  204. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sb + "Downloading " + fb + sb + "'%s'." % (course_path.replace('-',' ')))
  205. self.login()
  206. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading course information webpages ..")
  207. videos_dict = self.clean_dict(extract_info.real_extract(self.url, course_name, course_path))
  208. self.logout()
  209. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Extracting chapters & lectures information..")
  210. if isinstance(videos_dict, dict):
  211. for chap in sorted(videos_dict):
  212. print (fc + sd + "\n[" + fw + sb + "+" + fc + sd + "] : " + fw + sd + "Chapter (%s)" % (chap))
  213. for lecture,urls in sorted(videos_dict[chap].items()):
  214. try:
  215. _file = urls.get('file')
  216. _external_url = urls.get('external_url')
  217. _subtitle = urls.get('subtitle')
  218. except AttributeError as e:
  219. pass
  220. else:
  221. if _external_url and not _file and not _subtitle:
  222. _external_url = urls.get('external_url' )
  223. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fy + sb + "External Link '" + fm + sd + str(lecture)+ fy + sb + "'..")
  224. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fy + sb + "Visit " + fg + sd + "(" + str(_external_url)+ fg + sb + ")\n")
  225. elif _file and not _external_url and not _subtitle:
  226. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fy + sb + "File '" + fm + sd + str(lecture)+ fy + sb + "'..")
  227. print (fy + sb + "+--------------------------------------------+")
  228. print (fy + sb + "| {:<6} {:<8} {:<7} {:<15}|".format("Stream", "Type", "Format", "Size"))
  229. print (fy + sb + "| {:<6} {:<8} {:<7} {:<15}|".format("------", "-----", "------","-------"))
  230. sid = 1
  231. url = _file
  232. sz,in_MB = self.get_filsize(url)
  233. media = 'file'
  234. Format = lecture.split('.')[-1]
  235. print (fy + sb + "|" + fg + sd + " {:<6} {:<8} {:<7} {:<5} {:<8}{}{}|".format(sid, media, Format , sz, in_MB, fy, sb))
  236. print (fy + sb + "+--------------------------------------------+")
  237. elif _subtitle and not _file and not _external_url:
  238. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fy + sb + "Subtitle '" + fm + sd + str(lecture)+ fy + sb + "'..")
  239. print (fy + sb + "+--------------------------------------------+")
  240. print (fy + sb + "| {:<6} {:<8} {:<7} {:<15}|".format("Stream", "Type", "Format", "Size"))
  241. print (fy + sb + "| {:<6} {:<8} {:<7} {:<15}|".format("------", "-----", "------","-------"))
  242. sid = 1
  243. url = _subtitle
  244. sz,in_MB = self.get_filsize(url)
  245. media = 'file'
  246. Format = lecture.split('.')[-1]
  247. print (fy + sb + "|" + fg + sd + " {:<6} {:<8} {:<7} {:<5} {:<9}{}{}|".format(sid, media, Format , sz, in_MB, fy, sb))
  248. print (fy + sb + "+--------------------------------------------+")
  249. elif not _external_url and not _file and not _subtitle:
  250. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fy + sb + "Lecture '" + fm + sd + str(lecture)+ fy + sb + "'..")
  251. print (fy + sb + "+--------------------------------------------------------+")
  252. print (fy + sb + "| {:<6} {:<8} {:<7} {:<12} {:<14}|".format("Stream", "Type", "Format", "Quality", "Size"))
  253. print (fy + sb + "| {:<6} {:<8} {:<7} {:<10} {:<16}|".format("------", "-----", "------", "-------", "--------"))
  254. i = 0
  255. for _url,res in urls.items():
  256. sid = i + 1
  257. quality = res
  258. url = _url
  259. sz,in_MB = self.get_filsize(url)
  260. media = 'video'
  261. Format = 'mp4'
  262. print (fy + sb + "|" + fg + sd + " {:<6} {:<8} {:<7} {:<10} {:<7}{:<9}{}{}|".format(sid, media, Format , str(quality) + 'p', sz, in_MB, fy, sb))
  263. i += 1
  264. print (fy + sb + "+--------------------------------------------------------+")
  265.  
  266.  
  267. def ExtractAndDownload(self, path=None, quality=None, default=False):
  268. current_dir = os.getcwd()
  269. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading webpage..")
  270. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Extracting course information..")
  271. time.sleep(2)
  272. course = extract_info.match_id(self.url)
  273. if not path:
  274. course_path = extract_info.match_id(self.url)
  275. course_name = current_dir + '\\' + course_path if os.name == 'nt' else current_dir + '/' + course_path
  276. else:
  277. course_path = "%s\\%s" % (path, extract_info.match_id(self.url)) if os.name == 'nt' else "%s/%s" % (path, extract_info.match_id(self.url))
  278. course_name = course_path
  279.  
  280. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sb + "Downloading " + fb + sb + "'%s'." % (course.replace('-',' ')))
  281. self.login()
  282. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading course information webpages ..")
  283. videos_dict = self.clean_dict(extract_info.real_extract(self.url, course_name, course_path))
  284. self.logout()
  285. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Counting no of chapters..")
  286. if isinstance(videos_dict, dict):
  287. print (fc + sd + "[" + fm + sb + "+" + fc + sd + "] : " + fw + sd + "Found ('%s') chapter(s).\n" % (len(videos_dict)))
  288. j = 1
  289. for chap in sorted(videos_dict):
  290. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fm + sb + "Downloading chapter : (%s of %s)" % (j, len(videos_dict)))
  291. print (fc + sd + "[" + fw + sb + "+" + fc + sd + "] : " + fw + sd + "Chapter (%s)" % (chap))
  292. chapter_path = course_path + '\\' + chap if os.name == 'nt' else course_path + '/' + chap
  293. try:
  294. os.makedirs(chapter_path)
  295. except Exception as e:
  296. pass
  297. chapter_path = course_name + '\\' + chap if os.name == 'nt' else course_name + '/' + chap
  298. if os.path.exists(chapter_path):
  299. os.chdir(chapter_path)
  300. print (fc + sd + "[" + fm + sb + "+" + fc + sd + "] : " + fc + sd + "Found ('%s') lecture(s)." % (len(videos_dict[chap])))
  301. i = 1
  302. for lecture_name,urls in sorted(videos_dict[chap].items()):
  303. try:
  304. _file = urls.get('file' )
  305. _external_url = urls.get('external_url' )
  306. _subtitle = urls.get('subtitle' )
  307. except AttributeError as e:
  308. pass
  309. else:
  310. if _file and not _external_url and not _subtitle:
  311. _url = _file
  312. elif _subtitle and not _file and not _external_url:
  313. _url = _subtitle
  314. elif not _external_url and not _file and not _subtitle:
  315. if not quality:
  316. try:
  317. _url = max(urls, key=urls.get)
  318. except ValueError as e:
  319. continue
  320. else:
  321. found = False
  322. for url,res in urls.items():
  323. if res == quality:
  324. _url = url
  325. found = True
  326. else:
  327. continue
  328.  
  329. if not found:
  330. if default:
  331. print (fc + sd + "\n[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Request quality not found for (%s)" % (lecture_name))
  332. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fw + sb + "Downloading default quality...")
  333. try:
  334. _url = max(urls, key=urls.get)
  335. except ValueError as e:
  336. continue
  337. else:
  338. print (fc + sd + "\n[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Request quality is not available for (%s)" % (lecture_name))
  339. if version_info[:2] >= (3, 0):
  340. askUser = input(fc + sd + "[" + fw + sb + "?" + fc + sd + "] : " + fw + sb + "Would you like to download the default quality : ")
  341. else:
  342. askUser = raw_input(fc + sd + "[" + fw + sb + "?" + fc + sd + "] : " + fw + sb + "Would you like to download the default quality : ")
  343. if askUser == 'y' or askUser == 'Y' or askUser == '':
  344. try:
  345. _url = max(urls, key=urls.get)
  346. except ValueError as e:
  347. continue
  348. else:
  349. i += 1
  350. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Continuing to check for the next lecture..")
  351. continue
  352. if _external_url and not _file:
  353. _url = _external_url
  354. _external_links = "links-to-visit.txt"
  355. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Saving external links to file : {}".format(_external_links))
  356. f = open(_external_links, "a")
  357. # print(_external_links)
  358. f.write("[+] -- name {}\n[+] -- Visit {}\n".format(lecture_name, _url))
  359. f.close()
  360. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Saved successfully..")
  361. elif _url:
  362. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading lecture : (%s of %s)" % (i, len(videos_dict[chap])))
  363. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Downloading (%s)" % (lecture_name))
  364. if '.mp4' in _url:
  365. pass
  366. else:
  367. out = self.Downloader(_url, lecture_name, chapter_path)
  368. if out == 'already_exist':
  369. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Lecture : '%s' " % (lecture_name) + fy + sb + "(already downloaded).")
  370. else:
  371. print (fc + sd + "\n[" + fm + sb + "+" + fc + sd + "] : " + fg + sd + "Downloaded (%s)" % (lecture_name))
  372. i += 1
  373. j += 1
  374. print ('')
  375. os.chdir(current_dir)
  376.  
  377.  
  378.  
  379.  
  380.  
  381. def main():
  382. ban = banner()
  383. print (ban)
  384. usage = '''%prog [-h] [-u "username"] [-p "password"] COURSE_URL
  385. [-s] [-l] [-r VIDEO_QUALITY] [-o OUTPUT] [-d]
  386. [--configs]'''
  387. version = "%prog version {}".format(__version__)
  388. description = 'A cross-platform python based utility to download courses from udemy for personal offline use.'
  389. parser = optparse.OptionParser(usage=usage,version=version,conflict_handler="resolve", description=description)
  390.  
  391. general = optparse.OptionGroup(parser, 'General')
  392. general.add_option(
  393. '-h', '--help',
  394. action='help',
  395. help='Shows the help.')
  396. general.add_option(
  397. '-v', '--version',
  398. action='version',
  399. help='Shows the version.')
  400.  
  401. downloader = optparse.OptionGroup(parser, "Advance")
  402. downloader.add_option(
  403. "-u", "--username",
  404. action='store_true',
  405. dest='email',\
  406. help="Username in udemy.")
  407. downloader.add_option(
  408. "-p", "--password",
  409. action='store_true',
  410. dest='password',\
  411. help="Password of your account.")
  412. downloader.add_option(
  413. "-c","--configs",
  414. action='store_true',
  415. dest='configurations',\
  416. help="Cache your credentials to use it later.")
  417. downloader.add_option(
  418. "-s", "--save-links",
  419. action='store_true',
  420. dest='save_links',\
  421. help="Do not download but save links to a file.")
  422. downloader.add_option(
  423. "-l", "--list-infos",
  424. action='store_true',
  425. dest='list',\
  426. help="List all lectures with available resolution.")
  427. downloader.add_option(
  428. "-r", "--resolution",
  429. action='store_true',
  430. dest='quality',\
  431. help="Download video resolution, default resolution is 720p.")
  432. downloader.add_option(
  433. "-d", "--get-default",
  434. action='store_true',
  435. default = False,
  436. dest='default',\
  437. help="Download default resolution if requested not there.")
  438. downloader.add_option(
  439. "-o", "--output",
  440. action='store_true',
  441. dest='output',\
  442. help="Output directory where the videos will be saved, default is current directory.")
  443.  
  444.  
  445. parser.add_option_group(general)
  446. parser.add_option_group(downloader)
  447.  
  448. (options, args) = parser.parse_args()
  449.  
  450. if not options.email and not options.password:
  451. try:
  452. url = args[0]
  453. except IndexError as e:
  454. parser.print_help()
  455. else:
  456. config = use_cached_creds()
  457. if isinstance(config, dict):
  458. email = config.get('username')
  459. passwd = config.get('password')
  460. resolution = config.get('resolution') or None
  461. output = config.get('output') or None
  462.  
  463. if resolution != "" and resolution != None:
  464. options.quality = True
  465. options.default = True
  466. else:
  467. options.quality = False
  468.  
  469. if output != "" and output != None:
  470. options.output = True
  471. else:
  472. options.output = False
  473.  
  474. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Using cached configurations..")
  475. if email != "" and passwd != "":
  476. udemy = UdemyDownload(url, email, passwd)
  477. else:
  478. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Username and password seems empty in 'configuration' file")
  479. username = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Username : " + fg + sb
  480. password = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Password : " + fc + sb
  481. email = input(username) if version_info[:2] >= (3, 0) else raw_input(username)
  482. passwd = getpass.getpass(prompt=password)
  483. print ("")
  484. if email and passwd:
  485. udemy = UdemyDownload(url, email, passwd)
  486. else:
  487. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Username and password is required..")
  488. exit(0)
  489. if options.configurations:
  490. pass
  491. ''' Download course '''
  492. if not options.save_links and not options.list and not options.output and not options.quality:
  493. udemy.ExtractAndDownload()
  494. elif not options.save_links and not options.list and options.output and not options.quality:
  495. outto = output
  496. udemy.ExtractAndDownload(path=outto)
  497. elif not options.save_links and not options.list and options.output and options.quality:
  498. res = resolution
  499. outto = output
  500. if options.default:
  501. udemy.ExtractAndDownload(path=outto, quality=res, default=True)
  502. else:
  503. udemy.ExtractAndDownload(path=outto, quality=res)
  504. elif not options.save_links and not options.list and not options.output and options.quality:
  505. res = resolution
  506. if options.default:
  507. udemy.ExtractAndDownload(quality=res, default=True)
  508. else:
  509. udemy.ExtractAndDownload(quality=res)
  510. ''' Save course links '''
  511. elif options.save_links and not options.list and not options.output and not options.quality:
  512. udemy.SaveLinks()
  513. elif options.save_links and not options.list and not options.output and options.quality:
  514. res = resolution
  515. if options.default:
  516. udemy.SaveLinks(quality=res, default=True)
  517. else:
  518. udemy.SaveLinks(quality=res)
  519. elif options.save_links and not options.list and options.output and not options.quality:
  520. outto = output
  521. udemy.SaveLinks(path=outto)
  522. elif options.save_links and not options.list and options.output and options.quality:
  523. res = resolution
  524. outto = output
  525. if options.default:
  526. udemy.SaveLinks(quality=res, path=outto, default=True)
  527. else:
  528. udemy.SaveLinks(quality=res, path=outto)
  529. ''' list down available formats of files and videos '''
  530. elif options.list:
  531. udemy.ListDown()
  532.  
  533. else:
  534. username = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Username : " + fg + sb
  535. password = fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Password : " + fc + sb
  536. email = input(username) if version_info[:2] >= (3, 0) else raw_input(username)
  537. passwd = getpass.getpass(prompt=password)
  538. print ("")
  539. if options.configurations:
  540. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Caching configuration...")
  541. cached = cache_creds(email, passwd)
  542. if cached == 'cached':
  543. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Configurations cached successfully...")
  544.  
  545. if email and passwd:
  546. udemy = UdemyDownload(url, email, passwd)
  547. else:
  548. print (fc + sd + "[" + fr + sb + "-" + fc + sd + "] : " + fr + sb + "Username and password is required..")
  549. exit(0)
  550. ''' Download course '''
  551. if not options.save_links and not options.list and not options.output and not options.quality:
  552. udemy.ExtractAndDownload()
  553. elif not options.save_links and not options.list and options.output and not options.quality:
  554. outto = args[1]
  555. udemy.ExtractAndDownload(path=outto)
  556. elif not options.save_links and not options.list and options.output and options.quality:
  557. res = args[1]
  558. outto = args[2]
  559. if options.default:
  560. udemy.ExtractAndDownload(path=outto, quality=res, default=True)
  561. else:
  562. udemy.ExtractAndDownload(path=outto, quality=res)
  563. elif not options.save_links and not options.list and not options.output and options.quality:
  564. res = args[1]
  565. if options.default:
  566. udemy.ExtractAndDownload(quality=res, default=True)
  567. else:
  568. udemy.ExtractAndDownload(quality=res)
  569. ''' Save course links '''
  570. elif options.save_links and not options.list and not options.output and not options.quality:
  571. udemy.SaveLinks()
  572. elif options.save_links and not options.list and not options.output and options.quality:
  573. res = args[1]
  574. if options.default:
  575. udemy.SaveLinks(quality=res, default=True)
  576. else:
  577. udemy.SaveLinks(quality=res)
  578. elif options.save_links and not options.list and options.output and not options.quality:
  579. outto = args[1]
  580. udemy.SaveLinks(path=outto)
  581. elif options.save_links and not options.list and options.output and options.quality:
  582. res = args[1]
  583. outto = args[2]
  584. if options.default:
  585. udemy.SaveLinks(quality=res, path=outto, default=True)
  586. else:
  587. udemy.SaveLinks(quality=res, path=outto)
  588. ''' list down available formats of files and videos '''
  589. elif not options.save_links and options.list and not options.output and not options.quality:
  590. udemy.ListDown()
  591.  
  592. elif options.email and options.password:
  593. email = args[0]
  594. passwd = args[1]
  595. try:
  596. url = args[2]
  597. except IndexError as e:
  598. parser.print_usage()
  599. else:
  600. if options.configurations:
  601. print (fc + sd + "\n[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Caching configurations...")
  602. cached = cache_creds(email, passwd)
  603. if cached == 'cached':
  604. print (fc + sd + "[" + fm + sb + "*" + fc + sd + "] : " + fg + sd + "Configurations cached successfully...")
  605.  
  606. udemy = UdemyDownload(url, email, passwd)
  607. ''' Download course '''
  608. if not options.save_links and not options.list and not options.output and not options.quality:
  609. udemy.ExtractAndDownload()
  610. elif not options.save_links and not options.list and options.output and not options.quality:
  611. outto = args[3]
  612. udemy.ExtractAndDownload(path=outto)
  613. elif not options.save_links and not options.list and options.output and options.quality:
  614. res = args[3]
  615. outto = args[4]
  616. if options.default:
  617. udemy.ExtractAndDownload(path=outto, quality=res, default=True)
  618. else:
  619. udemy.ExtractAndDownload(path=outto, quality=res)
  620. elif not options.save_links and not options.list and not options.output and options.quality:
  621. res = args[3]
  622. if options.default:
  623. udemy.ExtractAndDownload(quality=res, default=True)
  624. else:
  625. udemy.ExtractAndDownload(quality=res)
  626. ''' Save course links '''
  627. elif options.save_links and not options.list and not options.output and not options.quality:
  628. udemy.SaveLinks()
  629. elif options.save_links and not options.list and not options.output and options.quality:
  630. res = args[3]
  631. if options.default:
  632. udemy.SaveLinks(quality=res, default=True)
  633. else:
  634. udemy.SaveLinks(quality=res)
  635. elif options.save_links and not options.list and options.output and not options.quality:
  636. outto = args[3]
  637. udemy.SaveLinks(path=outto)
  638. elif options.save_links and not options.list and options.output and options.quality:
  639. res = args[3]
  640. outto = args[4]
  641. if options.default:
  642. udemy.SaveLinks(quality=res, path=outto, default=True)
  643. else:
  644. udemy.SaveLinks(quality=res, path=outto)
  645. ''' list down available formats of files and videos '''
  646. elif not options.save_links and options.list and not options.output and not options.quality:
  647. udemy.ListDown()
  648.  
  649. if __name__ == '__main__':
  650. try:
  651. main()
  652. except KeyboardInterrupt:
  653. print (fc + sd + "\n[" + fr + sb + "-" + fc + sd + "] : " + fr + sd + "User Interrupted..")
  654. time.sleep(0.8)
  655. except IndexError:
  656. print (fc + sd + "\n[" + fr + sb + "-" + fc + sd + "] : " + fr + sd + "Username, Password and course url is required..")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement