Script for reconnect adsl in d-link router

In work I have wireless network with D-Link router “DSL-2640B”, and I need to reconnect ADSL service to change IP adress, so I write this ruby script, which use telnet standard libary.

#!/usr/bin/ruby
require 'net/telnet'
require 'optparse'

opts = ARGV.getopts('h:p:')

passwd = opts['-p']
host = opts['-h']

server = Net::Telnet::new('Host'=>host,
                          'Port'=> 23,
                          'Timeout'=>25,
                          'Prompt'=> />/)
server.waitfor(/ogin:/)
server.print("admin\n")
server.waitfor(/ssword:/)
server.print("#{passwd}\n")
server.cmd("adsl connection --down")
server.cmd("adsl connection --up")
server.cmd("logout")

You can use it in terminal by typing:

reconnect.rb -p <password> -h <host>

You must wait couple of seconds before your d-link router connect to ADSL.

Ściąganie plików z wrzuty

Aby ściągnąć pliki z wrzuty w Ruby, należy ze strony danego adresu odczytać zmienne javascript-u które zostaną przekazane do odtwarzacza napisanego we flashu.

require 'net/http'
require 'uri'
require 'rexml/document'

def get(url)
  url = URI.parse(url)
  http = Net::HTTP.new(url.host)
  return http.get(url.path).body
end

page = get(url)
#zczytanie zmiennych javascript
if page =~ /'key' : '([^']*)',/
  key = $1
else
  key = '4KWNcfGaCak'
end
if page =~ /'login' : '([^']*)',/
  login = $1
else
  login = 'lifthrasil'
end
if page =~ /'host' : '([^']*)',/
  host = $1
else
  host = 'labs.wrzuta.pl'
end
if page =~ /'site' : '([^']*)',/
  site = $1
else
  site = 'wrzuta.pl'
end
if page =~ /'lang' : '([^']*)',/
  lang = $1
end

Następnie z podanych zmiennych należy utworzyć adres url

rnd = (rand*1000000).floor
url = "http://#{login}.#{host}/xml/#{_local2}/#{key}/sa/#{site}/#{rnd}"

Pobrać daną stronę (jest to plik XML) i wczytać adres pliku

xml = REXML::Document.new(get(url)).root
url = xml.elements['//file/storeIds/fileId'][0]
#pobranie nazwy pliku
filename = xml.elements['//name'][0]

Następnie można użyć programu wget do ściągnięcia pliku

`wget -O #{filename} "#{url}"`

Możesz także skorzystać z Downloadera mój projekt na github, który ściąga pliki z przekleja, rapidshare, 4shared i wrzuty. Jak o używać możesz przeczytać w poście: File hosting downloader project (po angielsku) oraz skrypty do wszukiwania i walidacji linków z rapidshare.

Get you livebox router IP Adres

Here is bash on alias for get IP adress of your livebox router. (tested on Ubuntu GNU/Linux)

alias livebox-ip='tmp=`mktemp`;curl -u admin:<your password> --silent --url http://192.168.1.1/internetservices.html -o $tmp; grep -E "var v_Adrs_IP[^=]*=" $tmp | cut -d"\"" -f2 ; rm $tmp'

You must have installed curl. Note that you must change your livebox password.

File Hosting Downloader Project

I’ve create my first github project Downloader. It’s a Ruby script for download files from file hosting services. You can download files from 4shared, rapidshare, przeklej, filesonic and wrzuta. You can download it from github or from here.

You can use it:


download.rb <url>
or
download.rb -f <file with urls>

If you download files from rapidshare and you have Orange livebox, you can provide livebox possword for reconnection (change dynamic ADSL IP)


download.rb -l <livebox password> -f <filename>

If you download files from przeklej you can provide you user name and password if you want to download file larger than 50MB


download.rb -u <user> -p <password> -f <filename>

Change your IP with livebox

If you have livebox wireless router you could easily change you dynamic IP. It is handy if you download files from rapidshare. Here is the python code. It was tested on polish livebox. You could download code from here.

#!/usr/bin/python
import base64
import  urllib2
import urllib

def send(data, host, passwd):
    "send request to livebox SubmitInternetService."
    action = 'http://%s/SubmitInternetService' % host
    req = urllib2.Request(action, urllib.urlencode(data))
    passwd = base64.b64encode('%s:%s' % ('admin', passwd))
    req.add_header('Authorization',  'Basic %s' % passwd)
    request = urllib2.urlopen(req)
    return request.read()

def disconnect(host, passwd):
    "disconnect from internet."
    data = {'ACTION_DISCONNECT': 'Roz<span>&</span>#322;<span>&</span>#261;cz'}
    return send(data, host, passwd)

def connect(host, passwd):
    "connect to the internet."
    data = {'ACTION_CONNECT': 'Po<span>&</span>#322;<span>&</span>#261;cz'}
    return send(data, host, passwd)</pre>
You can use this functions like this.
<pre>if __name__ == '__main__':
    from sys import argv
    opts, rest = getopt(argv[1:], 'p:h:')
    opts = dict(opts)
    if len(rest) == 0:
        passwd = opts.get('-p', 'admin')
        host = opts.get('-h', '192.168.1.1')
        try:
            disconnect(host, passwd)
            connect(host, passwd)
        except urllib2.HTTPError, e:
            if e.code == 401:
                print 'wrong password'
            else:
                print 'Error: %s' % e.args
            exit(1)
        except urllib2.URLError, e:
            print 'Error: %s' % ' '.join(e.args)
        else:
            print "password successfuly changed"

You could use this progam like this:

change_ip.py -h <your host> -p <your password>

Default host is 192.168.1.1 and default password is admin

Dowload files from rapidshare

Here are function writen in Python for downloading files from Rapidshare. Unfortunetly you must wait couple of seconds, you can’t skip this because it seems that counting is on server side too.

from urllib2 import urlopen, Request
from urllib import urlencode
import os
import re
import time
import base64

class DownloadLimitException(Exception):
    def __init__(self, *arg):
        Exception.__init__(self, *arg)

def download(url):
    page = urlopen(url).read()
    action_url_regex = '<form id="[^"]*" action="([^"]*)"'
    s_url = re.search(action_url_regex, page)
    if s_url:
        url = s_url.group(1)
        data = urlencode({'dl.start': 'Free'})
        request = Request(url, data)
        page = urlopen(request).read()
        action_url_regex = '<form name="[^"]*" action="([^"]*)"'
        c_regex = 'var c=([0-9]*);'
        limit_regex = 'You have reached the download limit for free-users'
        if re.match(limit_regex, page):
            raise DownloadLimitException(limit_regex)
        s_url = re.search(action_url_regex, page)
        s_c = re.search(c_regex, page)
        if s_url and s_c:
            url = s_url.group(1)
            c = int(s_c.group(1))
            print "wait %i seconds" % c
            time.sleep(c)
            os.system('wget "%s"' % url)

You could use this function like this. Notice that you must have wget installed on your system.

def main():
    from sys import argv
    if len(argv) == 2:
        try:
            download(argv[1])
        except DownloadLimitException,
            print "limit reached"

if __name__ == '__main__':
    main()

Program for showing similar things

On tastekid.com you could find similar things like movies, book and music. They provide API which return XML or JSON response. Here are code, in Python which use this API. You could download source code from GitHub.

This program:

  • will display similar things if you use it without options
  • display information about movie/book/show/author if you put -i option
  • display descriptions if you use -d options
  • and translate them to your language with -l option.

Check -h options for more info.

#!/usr/bin/python
from urllib2 import urlopen, Request, HTTPError
from urllib import quote_plus
from sys import argv, stderr
from os.path import basename
from getopt import getopt, GetoptError
from StringIO import StringIO
import json

def gets(dict, *keys):
    "function generate values from dictionary which is on the list."  
    for e in keys:
	yield dict[e]

def partial_first(fun, *args):
    "return single argument function which execute function fun with arguments."
    def p(object):
	return fun(object, *args)
    return p

def gets_fun(*args):
    "return function which return values from dictinary which is on the list."
    return partial_first(gets, *args) 

class ServerJsonException(Exception):
    pass
	
class Similar(object):
    def __init__(self, stuff, type=None):
	query = quote_plus(stuff)
	if type:
	    query = '%s:%s' % (type, query)
	url = 'http://www.tastekid.com/ask/ws?&q=%s&format=JSON&verbose=1'
	response_data = urlopen(url % query).read()
	if not re.match('{.*}', response_data):
	    raise ServerJsonException
	#fix malform json
	response_data = response_data.replace('}{', '},{')
	self.data = json.load(StringIO(response_data))
	
    def infos(self):
	for i in self.data['Similar']['Info']:
	    yield Similar.Stuff(*list(gets(i, 'Name', 'Type', 'wTeaser')))

    def similar(self):
	"generate list of Stuff."
	results = self.data['Similar']['Results']
	if len(results) == 0:
	    yield None
	for result in results:
	    elems = list(gets(result, 'Name', 'Type', 'wTeaser', 'yTitle', 'yUrl'))
	    yield Similar.Stuff(*elems)
	    
    class Stuff(object):
	def __init__(self, name, type, description, y_title=None, y_url=None):
	    self.name = name.encode('UTF-8')
	    self.type = type.encode('UTF-8')
	    self.description = description.encode('UTF-8')
	    if y_title:
		self.y_title = y_title.encode('UTF-8')
	    if y_url:
		self.y_url = y_url.encode('UTF-8')

usage = """usage:
%s -d -i -y -l  
d - display descriptions
i - display only info
y - display youtube links
l - translate descriptions
    lang should be one of:
    af - afrikaans
    sk - albánskej
    ar - عربي
    be - Беларускі
    bg - Български
    zh - 荃湾
    zh - 太阳
    hr - Hrvatski
    cs - Český
    da - Danske
    et - Eesti
    tl - filipiński
    fi - Suomi
    fr - Français
    gl - galijski
    el - Ελληνικά
    iw - עברית
    hi - हिन्दी
    es - Español
    nl - Nederlands
    id - indonezyjski
    ga - Gaeilge
    is - Íslenska
    ja - 日本語
    yi - ייִדיש
    ca - Català
    ko - 한국의
    lt - Lietuvos
    lv - Latvijas
    mk - Македонски
    ms - Melayu
    mt - Malti
    de - Deutsch
    no - Norsk
    fa - فارسی
    pl - polski
    ru - Русский
    ro - Română
    sr - Српски
    sk - Slovenský
    sl - Slovenski
    sw - Swahili
    sv - Svenska
    th - ภาษาไทย
    tr - Türk
    uk - Український
    cy - walijski
    hu - Magyar
    vi - Việt
    it - Italiano

put "band:", "movie:", "show:", "book:" or "author:" before name if you want to specify search
""" % basename(argv[0])

def main():
    try:
        opts, rest = getopt(argv[1:], 'dl:iy')
    except GetoptError:
        print usage
        exit(1)
    opts = dict(opts)
    if opts.has_key('-h'):
        print usage
        exit(0)
    description = opts.has_key('-d')
    info = opts.has_key('-i')
    youtube = opts.has_key('-y')
    lang = opts.get('-l')
    if len(rest) == 0:
	print usage
    else:
	try:
	    stuff = Similar(' '.join(rest))
	    if info:
		for info in stuff.infos():
		    print '%s (%s)' % (info.name, info.type)
		    print
		    if lang:
			from xgoogle.translate import Translator
			translate = Translator().translate
			print translate(info.description, lang_to=lang)
		    else:
			print info.description
	    else:
		for stuff in stuff.similar():
		    print stuff.name
		    if youtube:
			print 'Youtube:'
			print '\t%s' % stuff.y_title
			print '\t%s' % stuff.y_url
		    if description:
			if lang:
			    from xgoogle.translate import Translator
			    translate = Translator().translate
			    print translate(stuff.description, lang_to=lang)
			else:
			    print stuff.description
	except ServerJsonException:
	    print >> stderr, "Error: can't read recived data from the server"


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        #when user hit Ctrl-C
        exit(1)

You need xgoogle library to it to work, you can download it from here.

You can use like this (you can change name of the script to like)

like Matrix
or
like Matrix, Ghost in the shell

If you want to check two or more movies/books/shows separate them with coma. If the same name has movie and book you can put type of things before the name

like movie:the gathering

like music:the gathering

allowed types are movie, show, book and author

if you want to download all youtube files use on Debian/Ubuntu

apt-get instll youtube-dl

or

wget http://www.nuxified.org/system/files/youtube-install.sh
sudo ./youtube-install.sh

and then run the script:

for i in `similar.py -y $1 | grep http`; do
    youtube-dl $i
done