cDownloader.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import json
  2. import codecs
  3. import datetime
  4. import os.path
  5. import logging
  6. import argparse
  7. import json
  8. import codecs
  9. from socket import timeout, error as SocketError
  10. from ssl import SSLError
  11. from urllib2 import URLError
  12. from httplib import HTTPException
  13. from instagram_private_api_extensions import live
  14. import sys, os, time, json
  15. import cLogger
  16. class NoBroadcastException(Exception):
  17. pass
  18. def main(apiArg, recordArg):
  19. global api
  20. global record
  21. api = apiArg
  22. record = recordArg
  23. getUserInfo(record)
  24. def recordStream(broadcast):
  25. try:
  26. dl = live.Downloader(
  27. mpd=broadcast['dash_playback_url'],
  28. output_dir='{}_output_{}/'.format(record, broadcast['id']),
  29. user_agent=api.user_agent)
  30. except Exception as e:
  31. cLogger.log('[E] Could not start recording broadcast: ' + str(e), "RED")
  32. cLogger.seperator("GREEN")
  33. exit()
  34. try:
  35. cLogger.log('[I] Starting broadcast recording.', "GREEN")
  36. dl.run()
  37. except KeyboardInterrupt:
  38. cLogger.log('', "GREEN")
  39. cLogger.log('[I] Aborting broadcast recording.', "GREEN")
  40. if not dl.is_aborted:
  41. dl.stop()
  42. finally:
  43. t = time.time()
  44. cLogger.log('[I] Stitching downloaded files into video.', "GREEN")
  45. dl.stitch(record + "_" + str(broadcast['id']) + "_" + str(int(t)) + '.mp4')
  46. cLogger.log('[I] Successfully stitched downloaded files.', "GREEN")
  47. cLogger.seperator("GREEN")
  48. exit()
  49. def getUserInfo(record):
  50. try:
  51. user_res = api.username_info(record)
  52. user_id = user_res['user']['pk']
  53. getBroadcast(user_id)
  54. except Exception as e:
  55. cLogger.log('[E] Could not get user info: ' + str(e), "RED")
  56. cLogger.seperator("GREEN")
  57. exit()
  58. def getBroadcast(user_id):
  59. try:
  60. cLogger.log('[I] Checking broadcast for "' + record + '".', "GREEN")
  61. broadcast = api.user_broadcast(user_id)
  62. if (broadcast is None):
  63. raise NoBroadcastException('No broadcast available.')
  64. else:
  65. recordStream(broadcast)
  66. except NoBroadcastException as e:
  67. cLogger.log('[W] Could not get broadcast info: ' + str(e), "YELLOW")
  68. cLogger.seperator("GREEN")
  69. exit()
  70. except Exception as e:
  71. if (e.__name__ is not NoBroadcastException):
  72. cLogger.log('[E] Could not get broadcast info: ' + str(e), "RED")
  73. cLogger.seperator("GREEN")
  74. exit()