startup.py 16 KB

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