Skip to content

Instantly share code, notes, and snippets.

@bactisme
Last active December 1, 2021 20:30
Show Gist options
  • Save bactisme/6414498 to your computer and use it in GitHub Desktop.
Save bactisme/6414498 to your computer and use it in GitHub Desktop.
Télécharge les dernières factures présentent sur l'admin OVH. Utilise l'API REST OVH. Tout cela en python ! Please do whatever you want to do with that.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# DOWNLOAD LAST BILLS ON OVH VIA REST API
import requests
from pprint import pprint
from lxml import html, etree
import StringIO
import json
import urllib2
import shutil
import urlparse
import os
import json
from hashlib import sha1
def do_ovh_credential(app_key, app_secret):
s = requests.Session()
data = { "accessRules": [{ "method": "GET", "path": "/*" },],
"redirection" : "http://example.org/"
}
d = json.dumps(data)
r = s.post("https://api.ovh.com/1.0/auth/credential", headers={"X-Ovh-Application" : app_key, "Content-type": "application/json"}, data=d)
d = json.loads(r.text)
ck = d['consumerKey']
vlurl = d['validationUrl']
print "Please visit this link "
print vlurl
return ck
def do_ovh(app_key, app_secret, ck, f):
try:
os.mkdir(f)
except: pass
s = requests.Session()
timestp = s.get("https://api.ovh.com/1.0/auth/time").text
url = "https://api.ovh.com/1.0/me/bill"
h = {
"X-Ovh-Application": app_key,
"X-Ovh-Timestamp": timestp,
"X-Ovh-Signature": "$1$" + sha1(app_secret + "+" + ck + "+" + "GET" + "+" + url + "+" + "" + "+" + timestp).hexdigest(),
"X-Ovh-Consumer": ck
}
fact = json.loads(s.get(url, headers=h).text)
for fl in fact:
url = "https://api.ovh.com/1.0/me/bill/"+fl
h = {
"X-Ovh-Application": app_key,
"X-Ovh-Timestamp": timestp,
"X-Ovh-Signature": "$1$" + sha1(app_secret + "+" + ck + "+" + "GET" + "+" + url + "+" + "" + "+" + timestp).hexdigest(),
"X-Ovh-Consumer": ck
}
r = s.get(url, headers=h)
d = json.loads(r.text)
download(d['pdfUrl'], s.cookies, f)
def download(url, cookies, folder):
req = requests.get(url, cookies=cookies)
localName = req.headers['Content-Disposition'].split('filename=')[1]
if localName[0] == '"' or localName[0] == "'":
localName = localName[1:-1]
localName = localName.replace(";", "")
print localName
f = open(folder + "/" + localName, 'wb')
f.write(req.content)
f.close()
def ovh_process(f):
print "1. Create an App "
print " -> http://www.ovh.com/cgi-bin/api/createApplication.cgi"
print ""
print "2. Get credentials"
print "App key"
AK = raw_input(" --> ")
print "App Secret"
AS = raw_input(" --> ")
print ""
print "3. Valid credentials"
CK = do_ovh_credential(AK, AS)
print " (Signature Token : " + CK + ")"
while(True):
s = raw_input("[ok] to continue, [q] to quit : ")
if s == "ok":
break
if s == "q":
exit(0)
do_ovh(AK, AS, CK, f)
if __name__ == '__main__':
ovh_process("ovh") # foldername
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment