startup.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. import argparse
  2. import configparser
  3. import os
  4. import logging
  5. import platform
  6. import subprocess
  7. try:
  8. import urlparse
  9. import pil
  10. import auth
  11. import logger
  12. import helpers
  13. import downloader
  14. import assembler
  15. import dlfuncs
  16. from constants import Constants
  17. import organize
  18. except ImportError:
  19. from urllib.parse import urlparse
  20. from . import pil
  21. from . import auth
  22. from . import logger
  23. from . import helpers
  24. from . import downloader
  25. from . import assembler
  26. from . import dlfuncs
  27. from .constants import Constants
  28. from . import organize
  29. def validate_inputs(config, args, unknown_args):
  30. error_arr = []
  31. banner_shown = False
  32. try:
  33. if args.configpath:
  34. if os.path.isfile(args.configpath):
  35. pil.config_path = args.configpath
  36. else:
  37. logger.banner()
  38. banner_shown = True
  39. logger.warn("Custom config path is invalid, falling back to default path: {:s}".format(pil.config_path))
  40. pil.config_path = os.path.join(os.getcwd(), "pyinstalive.ini")
  41. logger.separator()
  42. if not os.path.isfile(pil.config_path): # Create new config if it doesn't exist
  43. if not banner_shown:
  44. logger.banner()
  45. helpers.new_config()
  46. return False
  47. pil.config_path = os.path.realpath(pil.config_path)
  48. config.read(pil.config_path)
  49. if args.download:
  50. pil.dl_user = args.download
  51. if args.downloadfollowing or args.batchfile:
  52. logger.banner()
  53. logger.warn("Please use only one download method. Use -h for more information.")
  54. logger.separator()
  55. return False
  56. elif not args.clean and not args.info and not args.assemble and not args.downloadfollowing and not args.batchfile and not args.organize:
  57. logger.banner()
  58. logger.error("Please use a download method. Use -h for more information.")
  59. logger.separator()
  60. return False
  61. if helpers.bool_str_parse(config.get('pyinstalive', 'log_to_file')) == "Invalid":
  62. pil.log_to_file = True
  63. error_arr.append(['log_to_file', 'True'])
  64. elif helpers.bool_str_parse(config.get('pyinstalive', 'log_to_file')):
  65. pil.log_to_file = True
  66. else:
  67. pil.log_to_file = False
  68. logger.banner()
  69. if args.batchfile:
  70. if os.path.isfile(args.batchfile):
  71. pil.dl_batchusers = [user.rstrip('\n') for user in open(args.batchfile)]
  72. if not pil.dl_batchusers:
  73. logger.error("The specified file is empty.")
  74. logger.separator()
  75. return False
  76. else:
  77. logger.info("Downloading {:d} users from batch file.".format(len(pil.dl_batchusers)))
  78. logger.separator()
  79. else:
  80. logger.error('The specified file does not exist.')
  81. logger.separator()
  82. return False
  83. if unknown_args:
  84. pil.uargs = unknown_args
  85. logger.warn("The following unknown argument(s) were provided and will be ignored: ")
  86. logger.warn(' ' + ' '.join(unknown_args))
  87. logger.separator()
  88. pil.ig_user = config.get('pyinstalive', 'username')
  89. pil.ig_pass = config.get('pyinstalive', 'password')
  90. pil.dl_path = config.get('pyinstalive', 'download_path')
  91. pil.run_at_start = config.get('pyinstalive', 'run_at_start')
  92. pil.run_at_finish = config.get('pyinstalive', 'run_at_finish')
  93. pil.ffmpeg_path = config.get('pyinstalive', 'ffmpeg_path')
  94. pil.verbose = config.get('pyinstalive', 'verbose')
  95. pil.skip_merge = config.get('pyinstalive', 'skip_merge')
  96. pil.args = args
  97. pil.config = config
  98. pil.proxy = config.get('pyinstalive', 'proxy')
  99. if args.dlpath:
  100. pil.dl_path = args.dlpath
  101. if helpers.bool_str_parse(config.get('pyinstalive', 'show_cookie_expiry')) == "Invalid":
  102. pil.show_cookie_expiry = False
  103. error_arr.append(['show_cookie_expiry', 'False'])
  104. elif helpers.bool_str_parse(config.get('pyinstalive', 'show_cookie_expiry')):
  105. pil.show_cookie_expiry = True
  106. else:
  107. pil.show_cookie_expiry = False
  108. if helpers.bool_str_parse(config.get('pyinstalive', 'verbose')) == "Invalid":
  109. pil.verbose = False
  110. error_arr.append(['verbose', 'False'])
  111. elif helpers.bool_str_parse(config.get('pyinstalive', 'verbose')):
  112. pil.verbose = True
  113. else:
  114. pil.verbose = False
  115. if helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')) == "Invalid":
  116. pil.skip_merge = False
  117. error_arr.append(['skip_merge', 'False'])
  118. elif helpers.bool_str_parse(config.get('pyinstalive', 'skip_merge')):
  119. pil.skip_merge = True
  120. else:
  121. pil.skip_merge = False
  122. if helpers.bool_str_parse(config.get('pyinstalive', 'use_locks')) == "Invalid":
  123. pil.use_locks = False
  124. error_arr.append(['use_locks', 'False'])
  125. elif helpers.bool_str_parse(config.get('pyinstalive', 'use_locks')):
  126. pil.use_locks = True
  127. else:
  128. pil.use_locks = False
  129. if helpers.bool_str_parse(config.get('pyinstalive', 'clear_temp_files')) == "Invalid":
  130. pil.clear_temp_files = False
  131. error_arr.append(['clear_temp_files', 'False'])
  132. elif helpers.bool_str_parse(config.get('pyinstalive', 'clear_temp_files')):
  133. pil.clear_temp_files = True
  134. else:
  135. pil.clear_temp_files = False
  136. if helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')) == "Invalid":
  137. pil.do_heartbeat = True
  138. error_arr.append(['do_heartbeat', 'True'])
  139. if helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')):
  140. pil.do_heartbeat = True
  141. if args.noheartbeat or not helpers.bool_str_parse(config.get('pyinstalive', 'do_heartbeat')):
  142. pil.do_heartbeat = False
  143. logger.warn("Getting livestream heartbeat is disabled, this may cause degraded performance.")
  144. logger.separator()
  145. if not args.nolives and helpers.bool_str_parse(config.get('pyinstalive', 'download_lives')) == "Invalid":
  146. pil.dl_lives = True
  147. error_arr.append(['download_lives', 'True'])
  148. elif helpers.bool_str_parse(config.get('pyinstalive', 'download_lives')):
  149. pil.dl_lives = True
  150. else:
  151. pil.dl_lives = False
  152. if not args.noreplays and helpers.bool_str_parse(config.get('pyinstalive', 'download_replays')) == "Invalid":
  153. pil.dl_replays = True
  154. error_arr.append(['download_replays', 'True'])
  155. elif helpers.bool_str_parse(config.get('pyinstalive', 'download_replays')):
  156. pil.dl_replays = True
  157. else:
  158. pil.dl_replays = False
  159. if helpers.bool_str_parse(config.get('pyinstalive', 'download_comments')) == "Invalid":
  160. pil.dl_comments = True
  161. error_arr.append(['download_comments', 'True'])
  162. elif helpers.bool_str_parse(config.get('pyinstalive', 'download_comments')):
  163. pil.dl_comments = True
  164. else:
  165. pil.dl_comments = False
  166. if args.nolives:
  167. pil.dl_lives = False
  168. if args.noreplays:
  169. pil.dl_replays = False
  170. if args.verbose:
  171. pil.verbose = True
  172. if args.skip_merge:
  173. pil.skip_merge = True
  174. if not pil.dl_lives and not pil.dl_replays:
  175. logger.error("You have disabled both livestream and replay downloading.")
  176. logger.error("Please enable at least one of them and try again.")
  177. logger.separator()
  178. return False
  179. if pil.ffmpeg_path:
  180. if not os.path.isfile(pil.ffmpeg_path):
  181. pil.ffmpeg_path = None
  182. cmd = "where" if platform.system() == "Windows" else "which"
  183. logger.warn("Custom FFmpeg binary path is invalid, falling back to environment variable.")
  184. else:
  185. logger.binfo("Overriding FFmpeg binary path: {:s}".format(pil.ffmpeg_path))
  186. else:
  187. if not helpers.command_exists('ffmpeg') and not args.info:
  188. logger.error("FFmpeg framework not found, exiting.")
  189. logger.separator()
  190. return False
  191. if not pil.ig_user or not len(pil.ig_user):
  192. raise Exception("Invalid value for 'username'. This value is required.")
  193. if not pil.ig_pass or not len(pil.ig_pass):
  194. raise Exception("Invalid value for 'password'. This value is required.")
  195. if not pil.dl_path.endswith('/'):
  196. pil.dl_path = pil.dl_path + '/'
  197. if not pil.dl_path or not os.path.exists(pil.dl_path):
  198. pil.dl_path = os.getcwd() + "/"
  199. if not args.dlpath:
  200. error_arr.append(['download_path', os.getcwd() + "/"])
  201. else:
  202. logger.warn("Custom config path is invalid, falling back to default path: {:s}".format(pil.dl_path))
  203. logger.separator()
  204. if pil.proxy and pil.proxy != '':
  205. parsed_url = urlparse(pil.proxy)
  206. if not parsed_url.netloc or not parsed_url.scheme:
  207. error_arr.append(['proxy', 'None'])
  208. pil.proxy = None
  209. if error_arr:
  210. for error in error_arr:
  211. logger.warn("Invalid value for '{:s}'. Using default value: {:s}".format(error[0], error[1]))
  212. logger.separator()
  213. if args.info:
  214. helpers.show_info()
  215. return False
  216. elif args.clean:
  217. helpers.clean_download_dir()
  218. return False
  219. elif args.assemble:
  220. pil.assemble_arg = args.assemble
  221. assembler.assemble()
  222. return False
  223. elif args.organize:
  224. organize.organize_videos()
  225. return False
  226. return True
  227. except Exception as e:
  228. logger.error("An error occurred: {:s}".format(str(e)))
  229. logger.error("Make sure the config file and given arguments are valid and try again.")
  230. logger.separator()
  231. return False
  232. def run():
  233. pil.initialize()
  234. logging.disable(logging.CRITICAL)
  235. config = configparser.ConfigParser()
  236. parser = argparse.ArgumentParser(
  237. description="You are running PyInstaLive {:s} using Python {:s}".format(Constants.SCRIPT_VER,
  238. Constants.PYTHON_VER))
  239. parser.add_argument('-u', '--username', dest='username', type=str, required=False,
  240. help="Instagram username to login with.")
  241. parser.add_argument('-p', '--password', dest='password', type=str, required=False,
  242. help="Instagram password to login with.")
  243. parser.add_argument('-d', '--download', dest='download', type=str, required=False,
  244. help="The username of the user whose livestream or replay you want to save.")
  245. parser.add_argument('-b,', '--batch-file', dest='batchfile', type=str, required=False,
  246. help="Read a text file of usernames to download livestreams or replays from.")
  247. parser.add_argument('-i', '--info', dest='info', action='store_true', help="View information about PyInstaLive.")
  248. parser.add_argument('-nr', '--no-replays', dest='noreplays', action='store_true',
  249. help="When used, do not check for any available replays.")
  250. parser.add_argument('-nl', '--no-lives', dest='nolives', action='store_true',
  251. help="When used, do not check for any available livestreams.")
  252. parser.add_argument('-cl', '--clean', dest='clean', action='store_true',
  253. help="PyInstaLive will clean the current download folder of all leftover files.")
  254. parser.add_argument('-cp', '--config-path', dest='configpath', type=str, required=False,
  255. help="Path to a PyInstaLive configuration file.")
  256. parser.add_argument('-dp', '--download-path', dest='dlpath', type=str, required=False,
  257. help="Path to folder where PyInstaLive should save livestreams and replays.")
  258. parser.add_argument('-as', '--assemble', dest='assemble', type=str, required=False,
  259. help="Path to json file required by the assembler to generate a video file from the segments.")
  260. parser.add_argument('-df', '--download-following', dest='downloadfollowing', action='store_true',
  261. help="PyInstaLive will check for available livestreams and replays from users the account "
  262. "used to login follows.")
  263. parser.add_argument('-nhb', '--no-heartbeat', dest='noheartbeat', action='store_true', help="Disable heartbeat "
  264. "check for "
  265. "livestreams.")
  266. parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', help="PyInstaLive will output JSON "
  267. "responses and some misc "
  268. "variables.")
  269. parser.add_argument('-sm', '--skip-merge', dest='skip_merge', action='store_true', help="PyInstaLive will not merge the downloaded livestream files.")
  270. parser.add_argument('-o', '--organize', action='store_true', help="Create a folder for each user whose livestream(s) you have downloaded. The names of the folders will be their usernames. Then move the video(s) of each user into their associated folder.")
  271. # Workaround to 'disable' argument abbreviations
  272. parser.add_argument('--usernamx', help=argparse.SUPPRESS, metavar='IGNORE')
  273. parser.add_argument('--passworx', help=argparse.SUPPRESS, metavar='IGNORE')
  274. parser.add_argument('--infx', help=argparse.SUPPRESS, metavar='IGNORE')
  275. parser.add_argument('--noreplayx', help=argparse.SUPPRESS, metavar='IGNORE')
  276. parser.add_argument('--cleax', help=argparse.SUPPRESS, metavar='IGNORE')
  277. parser.add_argument('--downloadfollowinx', help=argparse.SUPPRESS, metavar='IGNORE')
  278. parser.add_argument('--configpatx', help=argparse.SUPPRESS, metavar='IGNORE')
  279. parser.add_argument('--confix', help=argparse.SUPPRESS, metavar='IGNORE')
  280. parser.add_argument('--organizx', help=argparse.SUPPRESS, metavar='IGNORE')
  281. parser.add_argument('-cx', help=argparse.SUPPRESS, metavar='IGNORE')
  282. parser.add_argument('-nx', help=argparse.SUPPRESS, metavar='IGNORE')
  283. parser.add_argument('-dx', help=argparse.SUPPRESS, metavar='IGNORE')
  284. args, unknown_args = parser.parse_known_args() # Parse arguments
  285. if validate_inputs(config, args, unknown_args):
  286. if not args.username and not args.password:
  287. pil.ig_api = auth.authenticate(username=pil.ig_user, password=pil.ig_pass)
  288. elif (args.username and not args.password) or (args.password and not args.username):
  289. logger.warn("Missing --username or --password argument. Falling back to config file.")
  290. logger.separator()
  291. pil.ig_api = auth.authenticate(username=pil.ig_user, password=pil.ig_pass)
  292. elif args.username and args.password:
  293. pil.ig_api = auth.authenticate(username=args.username, password=args.password, force_use_login_args=True)
  294. if pil.ig_api:
  295. if pil.dl_user or pil.args.downloadfollowing:
  296. downloader.start()
  297. elif pil.dl_batchusers:
  298. if not helpers.command_exists("pyinstalive"):
  299. logger.error("PyInstaLive must be properly installed when using the -b argument.")
  300. logger.separator()
  301. else:
  302. dlfuncs.iterate_users(pil.dl_batchusers)