Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to refresh ACD token with token from drivesink
#!/bin/env python
import json
import logging
import sys
import os
from six.moves import configparser
from datetime import datetime, timedelta
'''
Usage:
For the first run:
Run w3m https://drivesink.appspot.com/config to fill out the login form.
Maybe you would want check "Keep me signed in".
To refresh rclone token:
w3m -dump https://drivesink.appspot.com/config | python rclone_refresh.py
The token is usually valid for 1 hour, but you might want to refresh before
expiry.
Tested with Python 2 only. Should work with Python 3.
Any problems, send me email: rclone@yidong.im
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
# Path to rclone.conf.
# By default, for current user
CONFIG_PATH = os.path.expanduser('~/.config/rclone/rclone.conf')
# Name of the storage (remote)
STORAGE_NAME = 'acd'
# fd output from https://drivesink.appspot.com/config
input_file = sys.stdin
# Use level=logging.ERROR for less verbose
# Use level=DEBUG for more verbose
logging.basicConfig(level=logging.INFO)
# ----------- Code, don't touch ------------------
config = configparser.ConfigParser()
found_config = config.read(CONFIG_PATH)
logging.info("Found rclone config at %s"%found_config)
logging.debug("Current rclone config: %s"%config)
if not found_config:
logging.critical("No config file found")
exit()
if not config.has_section(STORAGE_NAME):
logging.critical("Storage %s doesn't exist")
exit()
logging.info("Read input token json")
token_json = None
for l in input_file:
if l[0] == '{':
token_json = l
break
for l in input_file:
token_json += l
if l[0] == '}':
break
logging.debug("Input token json: %s"%token_json)
token = json.loads(token_json)
logging.info("Input token will expire in %d seconds", token['expires_in'])
expiry_datetime = datetime.utcnow() + timedelta(seconds=token['expires_in'])
logging.info("Input token will expire at %s", expiry_datetime)
rclone_token = {
'access_token': token['access_token'],
'token_type': token['token_type'],
'refresh_token': token['refresh_token'],
'expiry': expiry_datetime.isoformat() + '+00:00'
}
rclone_token_json = json.dumps(rclone_token)
logging.info("New rclone token: %s"%rclone_token_json)
config.set(STORAGE_NAME, 'token', rclone_token_json)
logging.debug("New rclone config: %s"%config)
with open(CONFIG_PATH, 'w') as f:
config.write(f)
logging.info("Done")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment