Kaynağa Gözat

Remove old file, split functions

Cammy 7 yıl önce
ebeveyn
işleme
93da9d4287
10 değiştirilmiş dosya ile 234 ekleme ve 173 silme
  1. BIN
      authCookie.pyc
  2. 94 0
      cAuthCookie.py
  3. BIN
      cAuthCookie.pyc
  4. 82 0
      cDownloader.py
  5. BIN
      cDownloader.pyc
  6. 28 0
      cLogger.py
  7. BIN
      cLogger.pyc
  8. 29 0
      cMain.py
  9. 1 1
      looper.sh
  10. 0 172
      pyinstalive.py

BIN
authCookie.pyc


+ 94 - 0
cAuthCookie.py

@@ -0,0 +1,94 @@
+import json
+import codecs
+import datetime
+import os.path
+
+import cLogger
+
+try:
+    from instagram_private_api import (
+        Client, ClientError, ClientLoginError,
+        ClientCookieExpiredError, ClientLoginRequiredError,
+        __version__ as client_version)
+except ImportError:
+    import sys
+    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
+    from instagram_private_api import (
+        Client, ClientError, ClientLoginError,
+        ClientCookieExpiredError, ClientLoginRequiredError,
+        __version__ as client_version)
+
+
+def to_json(python_object):
+    if isinstance(python_object, bytes):
+        return {'__class__': 'bytes',
+                '__value__': codecs.encode(python_object, 'base64').decode()}
+    raise TypeError(repr(python_object) + ' is not JSON serializable')
+
+
+def from_json(json_object):
+    if '__class__' in json_object and json_object['__class__'] == 'bytes':
+        return codecs.decode(json_object['__value__'].encode(), 'base64')
+    return json_object
+
+
+def onlogin_callback(api, settings_file):
+    cache_settings = api.settings
+    with open(settings_file, 'w') as outfile:
+        json.dump(cache_settings, outfile, default=to_json)
+        cLogger.log('[I] New settings file was made: {0!s}'.format(settings_file), "GREEN")
+
+
+def login(username, password):
+
+    cLogger.log('PYINSTALIVE DOWNLOADER (API v{0!s})'.format(client_version), "GREEN")
+    cLogger.seperator("GREEN")
+
+    device_id = None
+    try:
+
+        settings_file = "credentials.json"
+        if not os.path.isfile(settings_file):
+            # settings file does not exist
+            cLogger.log('[W] Unable to find settings file: {0!s}'.format(settings_file), "YELLOW")
+
+            # login new
+            api = Client(
+                username, password,
+                on_login=lambda x: onlogin_callback(x, settings))
+        else:
+            with open(settings_file) as file_data:
+                cached_settings = json.load(file_data, object_hook=from_json)
+            # cLogger.log('[I] Using settings file: {0!s}'.format(settings_file), "GREEN")
+
+            device_id = cached_settings.get('device_id')
+            # reuse auth settings
+            api = Client(
+                username, password,
+                settings=cached_settings)
+
+    except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
+        cLogger.log('[E] ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e), "RED")
+
+        # Login expired
+        # Do relogin but use default ua, keys and such
+        api = Client(
+            username, password,
+            device_id=device_id,
+            on_login=lambda x: onlogin_callback(x, settings))
+
+    except ClientLoginError as e:
+        print('[E] ClientLoginError {0!s}'.format(e), "RED")
+        exit(9)
+    except ClientError as e:
+        print('[E] ClientError {0!s} (Code: {1:d}, Response: {2!s})'.format(e.msg, e.code, e.error_response), "RED")
+        exit(9)
+    except Exception as e:
+        print('[E] Unexpected Exception: {0!s}'.format(e), "RED")
+        exit(99)
+
+    # Show when login expires
+    # cookie_expiry = api.cookie_jar.expires_earliest
+    # print('[I] Cookie Expiry: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%dT%H:%M:%S')), "WHITE")
+    cLogger.log('[I] Login to "' + username + '" OK', "GREEN")
+    return api

BIN
cAuthCookie.pyc


+ 82 - 0
cDownloader.py

@@ -0,0 +1,82 @@
+import json
+import codecs
+import datetime
+import os.path
+import logging
+import argparse
+import json
+import codecs
+from socket import timeout, error as SocketError
+from ssl import SSLError
+from urllib2 import URLError
+from httplib import HTTPException
+from instagram_private_api_extensions import live
+import sys, os, time, json
+import cLogger
+
+class NoBroadcastException(Exception):
+    pass
+
+def main(apiArg, recordArg):
+	global api
+	global record
+	api = apiArg
+	record = recordArg
+	getUserInfo(record)
+
+def recordStream(broadcast):
+    try:
+        dl = live.Downloader(
+            mpd=broadcast['dash_playback_url'],
+            output_dir='{}_output_{}/'.format(record, broadcast['id']),
+            user_agent=api.user_agent)
+    except Exception as e:
+        cLogger.log('[E] Could not start recording broadcast: ' + str(e), "RED")
+        cLogger.seperator("GREEN")
+        exit()
+
+    try:
+        cLogger.log('[I] Starting broadcast recording.', "GREEN")
+        dl.run()
+    except KeyboardInterrupt:
+        cLogger.log('', "GREEN")
+        cLogger.log('[I] Aborting broadcast recording.', "GREEN")
+        if not dl.is_aborted:
+            dl.stop()
+    finally:
+        t = time.time()
+        cLogger.log('[I] Stitching downloaded files into video.', "GREEN")
+        dl.stitch(record + "_" + str(broadcast['id']) + "_" + str(int(t)) + '.mp4')
+        cLogger.log('[I] Successfully stitched downloaded files.', "GREEN")
+        cLogger.seperator("GREEN")
+        exit()
+
+
+def getUserInfo(record):
+    try:
+        user_res = api.username_info(record)
+        user_id = user_res['user']['pk']
+        getBroadcast(user_id)
+    except Exception as e:
+        cLogger.log('[E] Could not get user info: ' + str(e), "RED")
+        cLogger.seperator("GREEN")
+        exit()
+
+
+def getBroadcast(user_id):
+    try:
+        cLogger.log('[I] Checking broadcast for "' + record + '".', "GREEN")
+        broadcast = api.user_broadcast(user_id)
+        if (broadcast is None):
+            raise NoBroadcastException('No broadcast available.')
+        else:
+        	recordStream(broadcast)
+    except NoBroadcastException as e:
+        cLogger.log('[W] Could not get broadcast info: ' + str(e), "YELLOW")
+        cLogger.seperator("GREEN")
+        exit()
+    except Exception as e:
+    	if (e.__name__ is not NoBroadcastException):
+	        cLogger.log('[E] Could not get broadcast info: ' + str(e), "RED")
+	        cLogger.seperator("GREEN")
+	        exit()

BIN
cDownloader.pyc


+ 28 - 0
cLogger.py

@@ -0,0 +1,28 @@
+def colors(state):
+	color = ''
+
+	if (state == 'BLUE'):
+		color = '\033[94m'
+
+	if (state == 'GREEN'):
+		color = '\033[92m'
+
+	if (state == 'YELLOW'):
+		color = '\033[93m'
+
+	if (state == 'RED'):
+		color = '\033[91m'
+
+	if (state == 'ENDC'):
+		color = '\033[0m'
+
+	if (state == 'WHITE'):
+		color = '\033[0m'
+
+	return color
+
+def log(string, color):
+	print('\033[1m' + colors(color) + string + colors("ENDC"))
+
+def seperator(color):
+	print('\033[1m' + colors(color) + ("-" * 50) + colors("ENDC"))

BIN
cLogger.pyc


+ 29 - 0
cMain.py

@@ -0,0 +1,29 @@
+import codecs
+import datetime
+import os.path
+import logging
+import argparse
+import codecs
+import sys, os, time, json
+
+import cAuthCookie, cLogger, cDownloader
+
+from socket import timeout, error as SocketError
+from ssl import SSLError
+from urllib2 import URLError
+from httplib import HTTPException
+from instagram_private_api_extensions import live
+
+# "naxunaw" "pyinstalive" "credentials.json"
+
+global api, args, seperator
+parser = argparse.ArgumentParser(description='Login')
+parser.add_argument('-u', '--username', dest='username', type=str, required=True)
+parser.add_argument('-p', '--password', dest='password', type=str, required=True)
+parser.add_argument('-r', '--record', dest='record', type=str, required=True)
+
+args = parser.parse_args()
+
+api = cAuthCookie.login(args.username, args.password)
+
+cDownloader.main(api, args.record)

+ 1 - 1
looper.sh

@@ -1,5 +1,5 @@
 while true
 do
-python pyinstalive.py -u "naxunaw" -p "pyinstalive" -r "cen" -settings "credentials.json"
+python cMain.py -u "naxunaw" -p "pyinstalive" -r "simplerealistic"
 sleep 10
 done

+ 0 - 172
pyinstalive.py

@@ -1,172 +0,0 @@
-import json
-import codecs
-import datetime
-import os.path
-import logging
-import argparse
-import json
-import codecs
-from socket import timeout, error as SocketError
-from ssl import SSLError
-from urllib2 import URLError
-from httplib import HTTPException
-
-try:
-    from instagram_private_api import (
-        Client, ClientError, ClientLoginError,
-        ClientCookieExpiredError, ClientLoginRequiredError,
-        __version__ as client_version)
-except ImportError:
-    import sys
-    sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
-    from instagram_private_api import (
-        Client, ClientError, ClientLoginError,
-        ClientCookieExpiredError, ClientLoginRequiredError,
-        __version__ as client_version)
-
-from instagram_private_api_extensions import live
-import sys, os, time, json
-
-def download():
-    try:
-        user_res = api.username_info(args.record)
-        user_id = user_res['user']['pk']
-    except Exception as e:
-        print('[E] Could not get user information, exiting . . .')
-        print('[E] ' + str(e))
-        print(seperator)
-        exit()
-
-    try:
-        print('[I] Checking broadcast for ' + args.record + ' . . .')
-        broadcast = api.user_broadcast(user_id)
-        if (broadcast is None):
-            raise Exception('No broadcast available')
-    except Exception as e:
-        print('[E] Could not get broadcast information, exiting . . .')
-        print('[E] ' + str(e))
-        print(seperator)
-        exit()
-
-    try:
-        dl = live.Downloader(
-            mpd=broadcast['dash_playback_url'],
-            output_dir='{}_output_{}/'.format(args.record, broadcast['id']),
-            user_agent=api.user_agent)
-    except Exception as e:
-        print('[E] Could not start broadcast recording, exiting . . .')
-        print('[E] ' + str(e))
-        print(seperator)
-        exit()
-
-    try:
-        print('[I] Starting broadcast recording . . .')
-        dl.run()
-    except KeyboardInterrupt:
-        print('')
-        print('[I] Aborting broadcast recording . . .')
-        if not dl.is_aborted:
-            dl.stop()
-    finally:
-        t = time.time()
-        print('[I] Stitching downloaded files into video . . .')
-        dl.stitch(args.record + "_" + str(broadcast['id']) + "_" + str(int(t)) + '.mp4')
-        print('[I] Successfully stitched downloaded files, exiting . . .')
-        print(seperator)
-        exit()
-
-def to_json(python_object):
-    if isinstance(python_object, bytes):
-        return {'__class__': 'bytes',
-                '__value__': codecs.encode(python_object, 'base64').decode()}
-    raise TypeError(repr(python_object) + ' is not JSON serializable')
-
-
-def from_json(json_object):
-    if '__class__' in json_object and json_object['__class__'] == 'bytes':
-        return codecs.decode(json_object['__value__'].encode(), 'base64')
-    return json_object
-
-
-def onlogin_callback(api, new_settings_file):
-    cache_settings = api.settings
-    with open(new_settings_file, 'w') as outfile:
-        json.dump(cache_settings, outfile, default=to_json)
-        print('SAVED: {0!s}'.format(new_settings_file))
-
-
-if __name__ == '__main__':
-
-    logging.basicConfig()
-    logger = logging.getLogger('instagram_private_api')
-    logger.setLevel(logging.WARNING)
-
-    # Example command:
-    # python examples/savesettings_logincallback.py -u "yyy" -p "zzz" -settings "test_credentials.json"
-    parser = argparse.ArgumentParser(description='login callback and save settings demo')
-    parser.add_argument('-settings', '--settings', dest='settings_file_path', type=str, required=True)
-    parser.add_argument('-u', '--username', dest='username', type=str, required=True)
-    parser.add_argument('-p', '--password', dest='password', type=str, required=True)
-    parser.add_argument('-r', '--record', dest='record', type=str, required=True)
-    global args
-    global clear
-    global seperator
-    seperator = '=-' * 35
-    clear = lambda: os.system('clear')
-    args = parser.parse_args()
-    #clear()
-    print('PYINSTALIVE DOWNLOADER (API v{0!s})'.format(client_version))
-    print(seperator)
-
-    device_id = None
-    try:
-
-        settings_file = args.settings_file_path
-        if not os.path.isfile(settings_file):
-            # settings file does not exist
-            print('[E] Unable to find file: {0!s}'.format(settings_file))
-
-            # login new
-            api = Client(
-                args.username, args.password,
-                on_login=lambda x: onlogin_callback(x, args.settings_file_path))
-        else:
-            with open(settings_file) as file_data:
-                cached_settings = json.load(file_data, object_hook=from_json)
-            print('[I] Reusing settings: {0!s}'.format(settings_file))
-
-            device_id = cached_settings.get('device_id')
-            # reuse auth settings
-            api = Client(
-                args.username, args.password,
-                settings=cached_settings)
-
-    except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
-        print('[E] ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e))
-
-        # Login expired
-        # Do relogin but use default ua, keys and such
-        api = Client(
-            args.username, args.password,
-            device_id=device_id,
-            on_login=lambda x: onlogin_callback(x, args.settings_file_path))
-
-    except ClientLoginError as e:
-        print('[E] ClientLoginError {0!s}'.format(e))
-        print(seperator)
-        exit(9)
-    except ClientError as e:
-        print('[E] ClientError {0!s} (Code: {1:d}, Response: {2!s})'.format(e.msg, e.code, e.error_response))
-        print(seperator)
-        exit(9)
-    except Exception as e:
-        print('[E] Unexpected Exception: {0!s}'.format(e))
-        print(seperator)
-        exit(99)
-
-    # Show when login expires
-    cookie_expiry = api.cookie_jar.expires_earliest
-    print('[I] Cookie Expiry: {0!s}'.format(datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%dT%H:%M:%SZ')))
-
-print('[I] Successfully logged in, starting recorder . . .')
-download()