From 29045368407108569cfad4f6d6957297f9db162d Mon Sep 17 00:00:00 2001 From: Kirby Date: Thu, 12 Mar 2020 17:35:43 +0100 Subject: [PATCH] Adding scripts to work with bills, domain and retrieve consumer key --- .gitignore | 3 +++ ovh-domain.py | 26 ++++++++++++++++++++++++ ovh-factures.py | 35 ++++++++++++++++++++++++++++++++ ovh-getck.py | 18 +++++++++++++++++ ovh-mail.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+) create mode 100644 .gitignore create mode 100755 ovh-domain.py create mode 100755 ovh-factures.py create mode 100755 ovh-getck.py create mode 100755 ovh-mail.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cdddadc --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +data/* +*.yml +*.conf diff --git a/ovh-domain.py b/ovh-domain.py new file mode 100755 index 0000000..6803f5e --- /dev/null +++ b/ovh-domain.py @@ -0,0 +1,26 @@ +#!/usr/bin/python3 + +import requests +import ovh +import pycurl +import argparse + +try: + input = raw_input +except NameError: + pass + +parser = argparse.ArgumentParser(description='Process arguments') +parser.add_argument('domain', metavar='method', type=str, help='Function to use') +parser.add_argument('--method', metavar='fr', type=str, help='domain to request from') +args = parser.parse_args() + +# Instantiate. Visit https://api.ovh.com/createToken/?GET=/me +# to get your credentials +client = ovh.Client() + +# Grab bill list +zone = client.get('/domain/zone/' + args.domain + '/record') +for id in zone: + entry = client.get('/domain/zone/' + args.domain + '/record/'+str(id)) + print(entry) diff --git a/ovh-factures.py b/ovh-factures.py new file mode 100755 index 0000000..199b706 --- /dev/null +++ b/ovh-factures.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +import requests +import ovh +import pycurl +import yaml + +try: + input = raw_input +except NameError: + pass + +conf = yaml.load(open('creds.yml')) +user = conf['nextcloud']['username'] +password = conf['nextcloud']['password'] +client = ovh.Client() +bills = client.get('/me/bill') +for bill in bills: + url="https://nextcloud.hyrule.ovh/nextcloud/remote.php/dav/files/seb/Documents/Factures/OVH/" + details = client.get('/me/bill/%s' % bill) + r = requests.get(details['pdfUrl'], stream=True) + file_path = "/tmp/" + bill + ".pdf" + with open(file_path, 'wb') as f: + f.write(r.content) + f.close() + url += bill + ".pdf" + file = open(file_path) + c = pycurl.Curl() + c.setopt(c.URL, url) + c.setopt(c.USERPWD, '%s:%s' %(username, password)) + c.setopt(c.UPLOAD, 1) + c.setopt(c.READDATA, file) + c.perform() + c.close() + file.close() diff --git a/ovh-getck.py b/ovh-getck.py new file mode 100755 index 0000000..8b100b3 --- /dev/null +++ b/ovh-getck.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 +import requests +import ovh + +try: + input = raw_input +except NameError: + pass + +client = ovh.Client() +ck = client.new_consumer_key_request() +ck.add_recursive_rules(ovh.API_READ_WRITE, "/") +validation = ck.request() +print("Please visit %s to authenticate" % validation['validationUrl']) +input("and press Enter to continue...") +print("Welcome", client.get('/me')['firstname']) +print("Btw, your 'consumerKey' is '%s'" % validation['consumerKey']) + diff --git a/ovh-mail.py b/ovh-mail.py new file mode 100755 index 0000000..b900140 --- /dev/null +++ b/ovh-mail.py @@ -0,0 +1,53 @@ +#!/usr/bin/python3 + +import requests +import ovh +import pycurl +import json +import argparse + +def writeRedirectionsToFile(client): + # Grab bill list + mailbox = client.get('/email/domain/hyrule.ovh/redirection') + file = open(redir_file,'wt') + for id in mailbox: + redirection = client.get('/email/domain/hyrule.ovh/redirection/'+str(id)) + file.write(redirection['from']+','+redirection['to']+','+redirection['id']+'\n') + file.close() + +def checkRedirExists(client,fr,to): + mailbox = client.get('/email/domain/hyrule.ovh/redirection') + for id in mailbox: + redirection = client.get('/email/domain/hyrule.ovh/redirection/'+str(id)) + if (redirection['from'] == fr and redirection['to'] == to): + print("Redirection existante") + return id + elif (redirection['from'] == fr and redirection ['to'] != to): + print("Redirection existante vers l'adresse " + redirection['to']) + return id + else: + return False + +def addRedirection(client,fr,to): + result = client.post('/email/domain/hyrule.ovh/redirection',_from=fr,localCopy=False,to=to) + print(json.dumps(result,indent=4)) + return 0 + +parser = argparse.ArgumentParser(description='Process arguments') +parser.add_argument('method', metavar='method', type=str, help='Function to use') +parser.add_argument('--fr', metavar='fr', type=str, help='from mail address') +parser.add_argument('--to', metavar='to', type=str, help='to mail address') +args = parser.parse_args() + +redir_file = '/var/scripts/ovh_api/data/redirections.txt' + +try: + input = raw_input +except NameError: + pass +client = ovh.Client() +if (args.method == "write"): + writeRedirectionsToFile(client) +elif (args.method == "add"): + if not (checkRedirExists(client,args.fr,args.to)): + addRedirection(client,args.fr,args.to)