Created
May 13, 2011 22:26
-
-
Save alexisrobert/971426 to your computer and use it in GitHub Desktop.
Script Python quick'n'dirty qui permet de télécharger un lien sur la seedbox Freebox Révolution façon wget
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/python | |
# wgetfbx.py - Alexis ROBERT <[email protected]> | |
# Under WTFPL license. | |
# Puis jme la pete j'ecris en anglais aussi alors que c'est pour une Freebox. | |
# Puis jme la pete je mets une licence aussi. | |
# | |
# Si vous voulez conserver le mot de passe, creez un fichier | |
# .config/wgetfbx.conf du type : | |
# | |
# [wgetfbx] | |
# password = mot de passe | |
import urllib2, urllib | |
import cookielib | |
import sys, os.path | |
import getpass, json | |
from optparse import OptionParser | |
import ConfigParser | |
def launch_download(downurl): | |
reqmethod = "download.http_add" | |
reqstring = "http" | |
if options.bittorrent is True: | |
reqmethod = "download.torrent_add" | |
reqstring = "bittorrent" | |
req = urllib2.Request("http://mafreebox.freebox.fr/download.cgi", | |
urllib.urlencode({"url": downurl, "user": "freebox", | |
"method": reqmethod}), | |
{"Referer": "http://mafreebox.freebox.fr/download.php"}) | |
downreq = urllib2.urlopen(req) | |
if downreq.geturl() == "http://mafreebox.freebox.fr/download.cgi": | |
print "Erreur lors de la demande du telechargement." | |
return 1 | |
else: | |
print "Le telechargement de '%s' demarre (%s)." % (downurl, reqstring) | |
def show_download_progress(): | |
req = urllib2.Request("http://mafreebox.freebox.fr/download.cgi", | |
'{"jsonrpc": "2.0", "method": "download.list"}', | |
{"Referer": "http://mafreebox.freebox.fr/download.php", | |
"Content-Type": "application/json"}) | |
statusreq = urllib2.urlopen(req) | |
data = json.loads(statusreq.readline()) | |
print "Telechargements en cours :" | |
for item in data["result"]: | |
if item["status"] == "running": | |
if item["rx_rate"]/1024 > 1024: | |
speed = "%.2f Mo/s" % (float(item["rx_rate"])/(1024*1024)) | |
else: | |
speed = "%d Ko/s" % (item["rx_rate"]/1024) | |
if item["size"] == 0: | |
print "- %s : 0%%" | |
else: | |
print "- %s : %d%% (%s)" % (item["name"], | |
(item["transferred"]*100.0)/item["size"], | |
speed) | |
print "En pause :" | |
for item in data["result"]: | |
if item["status"] == "paused": | |
if item["size"] == 0: | |
print "- %s : 0%%" | |
else: | |
print "- %s : %d%%" % (item["name"], | |
(item["transferred"]*100.0)/item["size"]) | |
print "Telechargements termines :" | |
for item in data["result"]: | |
if item["status"] == "done": | |
print "- %s" % item["name"] | |
parser = OptionParser(usage="Usage: %prog [options] url") | |
parser.add_option("-b", "--bittorrent", action="store_true", dest="bittorrent", default=False, | |
help="mode Bittorrent") | |
(options, args) = parser.parse_args() | |
# Initialize cookie management | |
cj = cookielib.LWPCookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
urllib2.install_opener(opener) | |
# Fetch password from config if exists | |
c = ConfigParser.ConfigParser() | |
c.read(os.path.expanduser('~/.config/wgetfbx.conf')) | |
if c.has_section("wgetfbx") and c.has_option("wgetfbx", "password"): | |
password = c.get("wgetfbx", "password") | |
else: | |
# Else, fetch it from console | |
password = getpass.getpass("Mot de passe Freebox: ") | |
# Then, login | |
logreq = urllib2.urlopen("http://mafreebox.freebox.fr/login.php", | |
urllib.urlencode({"login":"freebox","passwd":password})) | |
if logreq.geturl() == "http://mafreebox.freebox.fr/login.php": | |
print "Mot de passe incorrect." | |
sys.exit(1) | |
if len(args) < 1: | |
sys.exit(show_download_progress()) | |
else: | |
sys.exit(launch_download(" ".join(args))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment