|
1 | 1 | # Author: Scott Philip (sp@scottphilip.com) |
2 | | -# Version: 0.4 (13 July 2017) |
| 2 | +# Version: 0.5 (24 July 2017) |
3 | 3 | # Source: https://github.com/scottphilip/google-token/ |
4 | 4 | # Licence: GNU GENERAL PUBLIC LICENSE (Version 3, 29 June 2007) |
| 5 | + |
5 | 6 | import json |
6 | 7 | from os.path import expanduser, join, isdir |
7 | 8 | from os import makedirs |
8 | 9 | from tempfile import gettempdir |
9 | 10 | from datetime import datetime |
10 | 11 |
|
11 | 12 |
|
12 | | -class GoogleTokenConfiguration: |
13 | | - """ |
14 | | - Configuration |
15 | | - """ |
| 13 | +class GoogleTokenConfiguration(object): |
| 14 | + account_email = None |
| 15 | + account_password = None |
| 16 | + account_otp_secret = None |
| 17 | + cookie_storage_path = None |
| 18 | + oauth_client_id = None |
| 19 | + oauth_redirect_uri = None |
| 20 | + oauth_scope = None |
| 21 | + logger = None |
| 22 | + image_path = None |
| 23 | + execute_script = None |
| 24 | + phantomjs_path = "phantomjs" |
| 25 | + phantomjs_config_useragent = "phantomjs.page.settings.userAgent" |
| 26 | + phantomjs_log_path = None |
| 27 | + cookies_ignore_discard = False |
| 28 | + cookies_store_plain = False |
| 29 | + url_accounts = "https://accounts.google.com" |
| 30 | + url_my_account = "https://myaccount.google.com" |
| 31 | + url_service_login = "https://accounts.google.com/ServiceLogin" |
| 32 | + url_accounts_no_form = "https://accounts.google.com/x" |
| 33 | + timeout_seconds = 30 |
| 34 | + oauth2_protocol = "https" |
| 35 | + oauth2_domain = "accounts.google.com" |
| 36 | + oauth2_path = "/o/oauth2/v2/auth" |
| 37 | + user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0" |
| 38 | + default_headers = None |
| 39 | + oauth2_data = None |
16 | 40 |
|
17 | | - def __init__(self, |
18 | | - account_email=None, |
19 | | - account_password=None, |
20 | | - account_otp_secret=None, |
21 | | - cookie_storage_path=None, |
22 | | - oauth_client_id=None, |
23 | | - oauth_redirect_uri=None, |
24 | | - oauth_scope=None, |
25 | | - logger=None, |
26 | | - image_path=None, |
27 | | - execute_script=None, |
28 | | - phantomjs_path="phantomjs", |
29 | | - phantomjs_config_useragent="phantomjs.page.settings.userAgent", |
30 | | - phantomjs_log_path=None, |
31 | | - cookies_ignore_discard=False, |
32 | | - url_accounts="https://accounts.google.com", |
33 | | - url_my_account="https://myaccount.google.com", |
34 | | - url_service_login="https://accounts.google.com/ServiceLogin", |
35 | | - url_accounts_no_form="https://accounts.google.com/x", |
36 | | - timeout_seconds=30, |
37 | | - oauth2_protocol="https", |
38 | | - oauth2_domain="accounts.google.com", |
39 | | - oauth2_path="/o/oauth2/v2/auth", |
40 | | - user_agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) Gecko/20100101 Firefox/25.0", |
41 | | - default_headers=None, |
42 | | - oauth2_data=None): |
| 41 | + def __init__(self, **kwargs): |
| 42 | + for key, value in kwargs.items(): |
| 43 | + if hasattr(self, key): |
| 44 | + setattr(self, key, value) |
| 45 | + continue |
| 46 | + raise Exception("Unknown Argument: {0}".format(key)) |
43 | 47 |
|
44 | | - self.account_email = account_email |
| 48 | + argument_missing = [] |
| 49 | + if self.account_email is None: |
| 50 | + argument_missing.append("account_email") |
| 51 | + if self.oauth_client_id is None: |
| 52 | + argument_missing.append("oauth_client_id") |
| 53 | + if self.oauth_redirect_uri is None: |
| 54 | + argument_missing.append("oauth_redirect_uri") |
| 55 | + if self.oauth_scope is None: |
| 56 | + argument_missing.append("oauth_scope") |
| 57 | + if len(argument_missing) > 0: |
| 58 | + raise Exception("REQUIRED_ARGUMENT", str(argument_missing)) |
45 | 59 |
|
46 | 60 | if not isdir(join(gettempdir(), "GoogleToken", self.account_email)): |
47 | 61 | makedirs(join(gettempdir(), "GoogleToken", self.account_email)) |
48 | | - if self.account_email is None: |
49 | | - raise Exception("account_email configuration must be set.") |
50 | | - self.account_password = account_password |
51 | | - self.account_otp_secret = account_otp_secret |
52 | | - self.cookie_storage_path = cookie_storage_path if cookie_storage_path is not None \ |
53 | | - else join(expanduser("~"), "{0}.cookies".format(self.account_email)) |
54 | | - self.oauth_client_id = oauth_client_id |
55 | | - self.oauth_redirect_uri = oauth_redirect_uri |
56 | | - self.oauth_scope = oauth_scope |
57 | | - self.logger = logger |
58 | | - self.image_path = image_path if image_path is not None else join( |
59 | | - gettempdir(), |
60 | | - "GoogleToken", |
61 | | - self.account_email, |
62 | | - datetime.now().strftime("%Y-%m-%d_%H-%M-%S")) |
63 | | - self.execute_script = execute_script |
64 | | - self.phantomjs_path = phantomjs_path |
65 | | - self.phantomjs_config_useragent = phantomjs_config_useragent |
66 | | - self.phantomjs_log_path = phantomjs_log_path if phantomjs_log_path is not None \ |
67 | | - else join(gettempdir(), |
68 | | - "GoogleToken", |
69 | | - self.account_email, "phantomjs.log") |
70 | | - self.cookies_ignore_discard = cookies_ignore_discard |
71 | | - self.url_accounts = url_accounts |
72 | | - self.url_my_account = url_my_account |
73 | | - self.url_service_login = url_service_login |
74 | | - self.url_accounts_no_form = url_accounts_no_form |
75 | | - self.timeout_seconds = timeout_seconds |
76 | | - self.oauth2_protocol = oauth2_protocol |
77 | | - self.oauth2_domain = oauth2_domain |
78 | | - self.oauth2_path = oauth2_path |
79 | | - self.user_agent = user_agent |
80 | | - self.default_headers = {"User-Agent": user_agent} if default_headers is None else default_headers |
| 62 | + |
| 63 | + self.cookie_storage_path = join(expanduser("~"), "{0}.cookies".format(self.account_email)) \ |
| 64 | + if self.cookie_storage_path is None else self.cookie_storage_path |
| 65 | + |
| 66 | + self.image_path = join(gettempdir(), "GoogleToken", self.account_email, datetime.now() |
| 67 | + .strftime("%Y-%m-%d_%H-%M-%S")) if \ |
| 68 | + self.image_path is None else self.image_path |
| 69 | + |
| 70 | + self.phantomjs_log_path = join(gettempdir(), "GoogleToken", self.account_email, "phantomjs.log") if \ |
| 71 | + self.phantomjs_log_path is None else self.phantomjs_log_path |
| 72 | + |
| 73 | + self.default_headers = {"User-Agent": self.user_agent} if self.default_headers is \ |
| 74 | + None else self.default_headers |
| 75 | + |
81 | 76 | self.oauth2_data = {"response_type": "token", |
82 | 77 | "client_id": "oauth_client_id", |
83 | 78 | "redirect_uri": "oauth_redirect_uri", |
84 | | - "scope": "oauth_scope"} if oauth2_data is None else oauth2_data |
| 79 | + "scope": "oauth_scope"} |
85 | 80 |
|
86 | 81 | def json(self): |
87 | 82 | result = {} |
|
0 commit comments