Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. def to_json(python_object):
  2. if isinstance(python_object, bytes):
  3. return {'__class__': 'bytes',
  4. '__value__': codecs.encode(python_object, 'base64').decode()}
  5. raise TypeError(repr(python_object) + ' is not JSON serializable')
  6.  
  7. def onlogin_callback(api, username):
  8. cache_settings = api.settings
  9. cookies.set(username, json.dumps(cache_settings, default=to_json))
  10. print('[I] New auth cookie file was made: {0!s}'.format(username))
  11.  
  12.  
  13. def login(username, password):
  14. device_id = None
  15. try:
  16. username_cookie = cookies.get(username)
  17. if not username_cookie:
  18. # settings file does not exist
  19. print('[W] Unable to find auth cookie file: {0!s} (creating a new one...)'.format(username))
  20. api = Client(
  21. username, password,
  22. on_login=lambda x: onlogin_callback(x, username))
  23. else:
  24. cached_settings = json.loads(username_cookie)
  25. device_id = cached_settings.get('device_id')
  26. # reuse auth settings
  27. api = Client(
  28. username, password,
  29. settings=json.dump(cached_settings, sys.stdout))
  30.  
  31. print('[I] Using cached login cookie for "' + api.authenticated_user_name + '".')
  32.  
  33. except (ClientCookieExpiredError, ClientLoginRequiredError) as e:
  34. print('[E] ClientCookieExpiredError/ClientLoginRequiredError: {0!s}'.format(e))
  35.  
  36. # Login expired
  37. # Do relogin but use default ua, keys and such
  38. if username and password:
  39. api = Client(
  40. username, password,
  41. device_id=device_id,
  42. on_login=lambda x: onlogin_callback(x, username_cookie))
  43.  
  44. except ClientLoginError as e:
  45. print('[E] Could not login: {:s}.n[E] {:s}nn{:s}'.format(
  46. json.loads(e.error_response).get("error_title", "Error title not available."),
  47. json.loads(e.error_response).get("message", "Not available"), e.error_response))
  48. print('-' * 70)
  49. sys.exit(9)
  50. except ClientError as e:
  51. print('[E] Client Error: {:s}'.format(e.error_response))
  52. print('-' * 70)
  53. sys.exit(9)
  54. except Exception as e:
  55. if str(e).startswith("unsupported pickle protocol"):
  56. print("[W] This cookie file is not compatible with Python {}.".format(sys.version.split(' ')[0][0]))
  57. print("[W] Please delete your cookie file 'credentials.json' and try again.")
  58. else:
  59. print('[E] Unexpected Exception: {0!s}'.format(e))
  60. print('-' * 70)
  61. sys.exit(99)
  62.  
  63. print('[I] Login to "' + api.authenticated_user_name + '" OK!')
  64. cookie_expiry = api.cookie_jar.auth_expires
  65. print('[I] Login cookie expiry date: {0!s}'.format(
  66. datetime.datetime.fromtimestamp(cookie_expiry).strftime('%Y-%m-%d at %I:%M:%S %p')))
  67.  
  68. return api
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement