|
| 1 | +#!/usr/bin/python3 |
| 2 | + |
| 3 | +# Copyright (C) 2023 Giovanni Fulco |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +import tidalapi |
| 19 | +import json |
| 20 | +import os |
| 21 | +from pathlib import Path |
| 22 | +from datetime import datetime |
| 23 | + |
| 24 | +tidal_plugin_name : str = "tidal" |
| 25 | + |
| 26 | + |
| 27 | +def print_setting(name : str, value : str): |
| 28 | + print(f"{name}={value}") |
| 29 | + |
| 30 | + |
| 31 | +tmp_directory : str = "generated" |
| 32 | +file_path : str = os.path.join("/tmp", tmp_directory) |
| 33 | +if not os.path.exists(file_path): |
| 34 | + os.makedirs(file_path) |
| 35 | + |
| 36 | +file_name = f"{file_path}/credentials.json" |
| 37 | + |
| 38 | +session = tidalapi.Session() |
| 39 | +# Will run until you visit the printed url and link your account |
| 40 | +session.login_oauth_simple() |
| 41 | + |
| 42 | +token_type = session.token_type |
| 43 | +access_token = session.access_token |
| 44 | +refresh_token = session.refresh_token |
| 45 | +expiry_time = session.expiry_time |
| 46 | + |
| 47 | +storable_expiry_time = datetime.timestamp(expiry_time) |
| 48 | + |
| 49 | +cred_dict: dict = dict() |
| 50 | +cred_dict["authentication_type"] = "oauth2" |
| 51 | +cred_dict["tokentype"] = "Bearer" |
| 52 | +cred_dict["accesstoken"] = access_token |
| 53 | +cred_dict["refreshtoken"] = refresh_token |
| 54 | +cred_dict["expirytimetimestampstr"] = storable_expiry_time |
| 55 | + |
| 56 | +print(json.dumps(cred_dict, indent=4, sort_keys=True)) |
| 57 | + |
| 58 | +print(f"Writing credentials to file [{file_name}] ...") |
| 59 | +with open(file_name, 'w') as wcf: |
| 60 | + json.dump(cred_dict, wcf, indent = 4) |
| 61 | +print(f"Credentials written to file [{file_name}].") |
0 commit comments