TinyURL Reverser

Stato
Discussione chiusa ad ulteriori risposte.

Robertof

Utente Silver
19 Giugno 2008
186
10
0
98
Gentili signori,
è con piacere che vi annuncio il mio nuovo script in Python fatto per provare le urllib2: TinyURL Reverser. Questo script, dandogli un url TinyURL, riesce a ricavare il link di origine.
Eccolo qui:
Codice:
#!/usr/bin/python

import urllib2
import sys
import re

if len(sys.argv) < 2:
	print "Use:\n\tpython " + sys.argv[0] + " http://tinyurl.com/theKey\nScript by Robertof - http://www.informarts.org"
else:
	regex = re.compile (r"http://(www\.){0,1}tinyurl.com/[0-9A-Za-z]{6}", re.I)
	stringa = sys.argv[1]
	a = regex.match (stringa)
	if a:
		# Match
		url = stringa.replace ("http://", "")
		url = url.replace ("www.", "")
		url = "http://preview." + str (url)
		lib = urllib2.urlopen (url)
		con = lib.read ()
		newRegex = re.compile (r"<blockquote><b>(.+)<br /></b></blockquote>")
		matches = newRegex.findall (con)
		if len (matches) == 1:
			print "Reversed URL is:\n\t" + str (matches[0])
		else:
			print "Unable to get URL"
	else:
		print "URL not valid"
 
Conosco poco il python quindi mi scuserai se non sono in grado di commentare il tuo codice.... posso solo commentare l'utilità dell'applicazione, e dire "ottima idea" :)
 
Plugin per emesene pronto!!!
Codice:
# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#    By Robertof - www.informarts.org 

VERSION = '0.1'
import Plugin
import re
import urllib2
import pynotify

class MainClass(Plugin.Plugin):
    '''Main plugin class'''
    
    def __init__(self, controller, msn):
        '''Contructor'''
        
        Plugin.Plugin.__init__(self, controller, msn)
        
        self.description = 'Ottieni l\'url di origine da un link di TinyURL facilmente.. '+\
        'Uso: /trev http://tinyurl.com/theKey'
        self.authors = { 'Roberto Frenna' : '[email protected]' }
        self.website = 'http://www.informarts.org'
        self.displayName = 'TinyURL Reverser'
        self.name = 'TinyURLreverser'
        self.controller = controller
        self.Slash = controller.Slash

    def start(self):
        '''start the plugin'''
        self.Slash.register('trev', self.reverse_url, 'Reverse URL')
        self.enabled = True

    def reverse_url(self, slash_action):
        parametri = slash_action.getParams()
        regex = re.compile (r"http://(www\.){0,1}tinyurl.com/[0-9A-Za-z]", re.I)
        if regex.match (parametri):
            url = parametri.replace ("http://", "")
            url = url.replace ("www.", "")
            url = "http://preview." + str (url)
            lib = urllib2.urlopen (url)
            con = lib.read ()
            newRegex = re.compile (r"<blockquote><b>(.+)<br /></b></blockquote>")
            matches = newRegex.findall (con)
            if len (matches) == 1:
                matches[0] = matches[0].replace ("<br />", "")
                matches[0] = matches[0].replace ("&amp;", "&")
                slash_action.outputText(_(matches[0]))
                try:
                    if pynotify.init("TinyURL Reverser"):
                        notifica = pynotify.Notification ("Informazione", "Il link di origine di " + str (parametri) + " è: " + str (matches[0]))
                        notifica.show ()
                    else:
                        slash_action.outputText(_('Non riesco a ricavare il link di origine'))
                except:
                    slash_action.outputText(_('Libnotify non e\' installato'))

    def stop(self):    
        '''stop the plugin'''
        self.Slash.unregister('trev')
        self.enabled = False

    def check( self ):
        return ( True, 'Ok' )
Oppure lo trovate qui:
http://www.wikifortio.com/741613/emesene.py

Piazzatelo in ~/.config/emesene1.0/pluginsEmesene, riavviate emesene e abilitate il plugin. Uso:
Codice:
/trev http://linktinyurl.com/theKey
ATTENZIONE: Prima di abilitare lo script è necessario avere il pacchetto «libnotify-bin»!
 
carino (programmi troppo ad oggetti per i miei gusti xD... poi magari mi ricrederò :p)
 
Roberto, una domanda, tu le quelle librerie le hai studiate dalla documentazione ufficiale? O puoi fornirmi qualche altra buona fonte?
 
Stato
Discussione chiusa ad ulteriori risposte.