Last active
August 5, 2018 20:01
-
-
Save Luzifer/7778459 to your computer and use it in GitHub Desktop.
Simple inofficial library to fetch Uberspace account data for monitoring money on account or whatever…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
from uberspace import * | |
import argparse, sys, math | |
ST_OK = 0 | |
ST_WARN = 1 | |
ST_CRIT = 2 | |
ST_UNKN = 3 | |
def main(): | |
parser = argparse.ArgumentParser(description = 'Check Uberspace account balance') | |
parser.add_argument('username', type=str, nargs=1, help='Uberspace username') | |
parser.add_argument('password', type=str, nargs=1, help='Uberspace password') | |
parser.add_argument('-c', '--critical', type=int, default=1, help='Number month to throw a critical if below threshold') | |
parser.add_argument('-w', '--warning', type=int, default=3, help='Number month to throw a warning if below threshold') | |
args = parser.parse_args() | |
try: | |
uberspace = Uberspace(args.username[0], args.password[0]) | |
data = uberspace.get_data() | |
except UberspaceException: | |
print 'CRITICAL: Login data incorrect!' | |
sys.exit(ST_CRIT) | |
except: | |
print 'UNKNOWN: Uberspace class threw an exception' | |
sys.exit(ST_UNKN) | |
months_left = float(data['current_amount']) / float(data['price']) | |
if months_left < args.critical: | |
print 'CRITICAL: %.2f EUR for account %s will only last for %d month(s) instead of at least %d month(s)' % (float(data['current_amount']), args.username[0], months_left, args.critical) | |
sys.exit(ST_CRIT) | |
elif months_left < args.warning: | |
print 'WARNING: %.2f EUR for account %s will only last for %d month(s) instead of at least %d month(s)' % (float(data['current_amount']), args.username[0], months_left, args.warning) | |
sys.exit(ST_WARN) | |
else: | |
print 'OK: %.2f EUR for account %s will last %d month(s)' % (float(data['current_amount']), args.username[0], months_left) | |
sys.exit(ST_OK) | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
class Uberspace: | |
def __init__(self, username, password): | |
self.sess = requests.Session() | |
self.login(username, password) | |
def login(self, username, password): | |
soup = BeautifulSoup(self.sess.post( | |
'https://uberspace.de/login', | |
data={ | |
'login': username, | |
'password': password, | |
'submit': 'anmelden', | |
}, | |
).text, 'html.parser') | |
if not username in soup.head.find('title').text: | |
raise UberspaceException('Login not succeeded.') | |
def get_data(self): | |
accountinfo = self.sess.get( | |
'https://uberspace.de/dashboard/accountinfo?format=json', | |
).json() | |
if 'login' not in accountinfo or accountinfo['login'] != self.username: | |
raise UberspaceException( | |
'Account data confused me. Login is not consistent.') | |
return accountinfo | |
def get_available_invoices(self): | |
soup = BeautifulSoup(self.sess.get( | |
'https://uberspace.de/dashboard/accounting', | |
).text, 'html.parser') | |
invoices = [] | |
for link in soup.find(attrs={'id': 'transactions'}).find_all('a'): | |
if link.has_attr('onclick') and link['href'] == '#': | |
# Found a changeable invoice | |
invoices.append({ | |
'id': link['onclick'].split('-')[1], | |
'fixed': False, | |
}) | |
else: | |
# Found a not changeable invoice | |
invoices.append({ | |
'id': link['href'].split('/')[-2], | |
'fixed': True, | |
}) | |
return invoices | |
def download_invoice(self, invoice, target, address=None): | |
url = 'https://uberspace.de/transaction/{id}/receipt'.format(**invoice) | |
resp = None | |
if invoice['fixed']: | |
resp = self.sess.get(url) | |
else: | |
resp = self.sess.post( | |
url, | |
data={ | |
'_csrf_token': self._get_csrf_token(), | |
'recipient': address, | |
'submit': 'Quittung erstellen', | |
}, | |
) | |
with open(target, 'wb') as f: | |
f.write(resp.content) | |
def _get_csrf_token(self): | |
soup = BeautifulSoup(self.sess.get( | |
'https://uberspace.de/dashboard/accounting', | |
).text, 'html.parser') | |
return soup.find('input', attrs={'name': '_csrf_token'})['value'] | |
class UberspaceException(Exception): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment