Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fetch_logbook.py #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions fetch_logbook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import requests
import json
import shutil
from get_accesstoken import getAccessToken
from secret import username


def request_data(n=0, access_kwargs={}):
'''
Request logbook from moonboard api

Args:
n (int): download 'startline'. Not used here. In case of >5000 lines?
access_kwargs (dict): dict passed to getAccessToken as kwargs

access_kwargs can be used to pass username and password as kwargs to
getAccessToken/getRefreshToken
'''
URL = f"https://restapimoonboard.ems-x.com/v1/_moonapi/Logbook/{n}?v=8.3.4"

headers = {
'accept-encoding': 'gzip, gzip',
'authorization': f'BEARER {getAccessToken(**access_kwargs)}',
'host': 'restapimoonboard.ems-x.com',
'user-agent': 'MoonBoard/1.0',
}

json = requests.get(URL, headers=headers).json()
return json


def main():
# Request data
json_data = request_data()

# Save to json file
with open("logbook.json", 'w') as file:
json.dump(json_data, file, indent=4)

# Copy to file with username in it
shutil.copy("logbook.json", f"logbook_{username}.json")


if __name__ == '__main__':
main()
19 changes: 13 additions & 6 deletions get_accesstoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import secret


def getAccessToken():
def getAccessToken(**kwargs):
''' Get 'access token' '''
URL = "https://restapimoonboard.ems-x.com/token"

headers = {
Expand All @@ -12,15 +13,21 @@ def getAccessToken():
'user-agent': 'MoonBoard/1.0',
}
data = {
'refresh_token': getRefreshToken(),
'refresh_token': getRefreshToken(**kwargs),
'grant_type': 'refresh_token',
'client_id': 'com.moonclimbing.mb'
}
r = requests.get(URL, headers=headers, data=data)
return r.json()['access_token']


def getRefreshToken():
def getRefreshToken(username=secret.username, password=secret.password):
''' Get a 'refresh token', used to obtaion additional 'access tokens'

Args:
username (str): username, default from secret.username
password (str): password, default from secret.password
'''
URL = "https://restapimoonboard.ems-x.com/token"

headers = {
Expand All @@ -30,10 +37,10 @@ def getRefreshToken():
'user-agent': 'MoonBoard/1.0',
}
data = {
'username': secret.username,
'password': secret.password,
'username': username,
'password': password,
'grant_type': 'password',
'client_id': 'com.moonclimbing.mb'
}
r = requests.get(URL, headers=headers, data=data)
return(r.json()['refresh_token'])
return (r.json()['refresh_token'])