Commits

Luiz Augusto Morais committed ab74cad

Programa para extração de urls de páginas de segurança.

  • Participants

Comments (0)

Files changed (3)

+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="2.16"/>
+  <!-- interface-naming-policy project-wide -->
+  <object class="GtkAboutDialog" id="sobre">
+    <property name="border_width">5</property>
+    <property name="title" translatable="yes">Sobre o URL PyExtractor</property>
+    <property name="window_position">center</property>
+    <property name="icon">urlpyextractor_icon_small.png</property>
+    <property name="type_hint">normal</property>
+    <property name="program_name">URL PyExtractor</property>
+    <property name="version">1.2</property>
+    <property name="copyright" translatable="yes">Copyright (C) 2010 - Luiz Augusto de Macêdo Morais</property>
+    <property name="comments" translatable="yes">Software que auxilia na extração das URLs de páginas de segurança.</property>
+    <property name="website">http://urlpyextractor.sourceforge.net/</property>
+    <property name="website_label" translatable="yes">Site do projeto</property>
+    <property name="license" translatable="yes">Este programa é um software livre; você pode redistribuí-lo e/ou modificá-lo
+sob os termos da GNU General Public License (GPL) como publicada pela
+Free Software Foundation; tanto na versão 2 da Licença ou (caso queira)
+qualquer versão posterior.</property>
+    <property name="authors">Luiz Augusto de Macêdo Morais</property>
+    <property name="logo">urlpyextractor_icon_small.png</property>
+    <child internal-child="vbox">
+      <object class="GtkVBox" id="dialog-vbox1">
+        <property name="visible">True</property>
+        <property name="spacing">2</property>
+        <child>
+          <placeholder/>
+        </child>
+        <child internal-child="action_area">
+          <object class="GtkHButtonBox" id="dialog-action_area1">
+            <property name="visible">True</property>
+            <property name="layout_style">end</property>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="pack_type">end</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>

gui/url_pyextractor.glade

+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="2.16"/>
+  <!-- interface-naming-policy project-wide -->
+  <object class="GtkWindow" id="janela">
+    <property name="width_request">500</property>
+    <property name="border_width">2</property>
+    <property name="title" translatable="yes">URL PyExtractor 1.2</property>
+    <property name="resizable">False</property>
+    <property name="window_position">center</property>
+    <property name="default_width">500</property>
+    <property name="default_height">150</property>
+    <property name="icon">urlpyextractor_icon_small.png</property>
+    <signal name="destroy" handler="sair"/>
+    <child>
+      <object class="GtkVBox" id="vbox1">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkMenuBar" id="menu">
+            <property name="visible">True</property>
+            <child>
+              <object class="GtkMenuItem" id="arquivo">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">_Arquivo</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="mnu_arquivo">
+                    <property name="visible">True</property>
+                    <child>
+                      <object class="GtkImageMenuItem" id="item_sair">
+                        <property name="label">gtk-quit</property>
+                        <property name="visible">True</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                        <signal name="activate" handler="sair"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkMenuItem" id="ajuda">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Aj_uda</property>
+                <property name="use_underline">True</property>
+                <child type="submenu">
+                  <object class="GtkMenu" id="mnu_ajuda">
+                    <property name="visible">True</property>
+                    <child>
+                      <object class="GtkImageMenuItem" id="item_sobre">
+                        <property name="label">gtk-about</property>
+                        <property name="visible">True</property>
+                        <property name="use_underline">True</property>
+                        <property name="use_stock">True</property>
+                        <signal name="hide" handler="esconder_sobre"/>
+                        <signal name="activate" handler="abrir_sobre"/>
+                      </object>
+                    </child>
+                  </object>
+                </child>
+              </object>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkVBox" id="vbox2">
+            <property name="visible">True</property>
+            <property name="border_width">4</property>
+            <child>
+              <object class="GtkHBox" id="hbox1">
+                <property name="visible">True</property>
+                <child>
+                  <object class="GtkLabel" id="url">
+                    <property name="visible">True</property>
+                    <property name="label" translatable="yes">Url: </property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkEntry" id="in_url">
+                    <property name="visible">True</property>
+                    <property name="can_focus">True</property>
+                    <property name="invisible_char">•</property>
+                    <property name="text" translatable="yes">http://</property>
+                  </object>
+                  <packing>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="padding">4</property>
+                <property name="position">0</property>
+              </packing>
+            </child>
+            <child>
+              <object class="GtkHBox" id="hbox2">
+                <property name="visible">True</property>
+                <child>
+                  <object class="GtkLabel" id="status">
+                    <property name="visible">True</property>
+                    <property name="xalign">0</property>
+                    <property name="selectable">True</property>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="position">0</property>
+                  </packing>
+                </child>
+                <child>
+                  <object class="GtkHButtonBox" id="hbuttonbox1">
+                    <property name="visible">True</property>
+                    <property name="spacing">1</property>
+                    <property name="layout_style">end</property>
+                    <child>
+                      <object class="GtkButton" id="bt_encurtar">
+                        <property name="label" translatable="yes">E_ncurtar</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">True</property>
+                        <property name="use_underline">True</property>
+                        <signal name="clicked" handler="encurtar"/>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">0</property>
+                      </packing>
+                    </child>
+                    <child>
+                      <object class="GtkButton" id="bt_extrair">
+                        <property name="label" translatable="yes">_Extrair</property>
+                        <property name="visible">True</property>
+                        <property name="can_focus">True</property>
+                        <property name="receives_default">True</property>
+                        <property name="use_underline">True</property>
+                        <signal name="clicked" handler="extrair"/>
+                      </object>
+                      <packing>
+                        <property name="expand">False</property>
+                        <property name="fill">False</property>
+                        <property name="position">1</property>
+                      </packing>
+                    </child>
+                  </object>
+                  <packing>
+                    <property name="expand">False</property>
+                    <property name="fill">False</property>
+                    <property name="padding">5</property>
+                    <property name="pack_type">end</property>
+                    <property name="position">1</property>
+                  </packing>
+                </child>
+              </object>
+              <packing>
+                <property name="padding">5</property>
+                <property name="position">1</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="position">1</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>

url_pyextractor.py

+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+# url_pyextractor.py
+
+import gtk
+import urllib
+import webbrowser as wb
+
+__author__ = 'Luiz Augusto de Macêdo Morais'
+__email__ = 'contato@ola-mundo.com'
+
+__license__ = 'GPL'
+__version__ = '1.1'
+__maintainer__ = 'Luiz Augusto de Macêdo Morais'
+
+
+class Pyextractor:
+    glade_file = 'gui/url_pyextractor.glade'
+    def __init__(self, *args):
+        # Criando a classe Builder
+        builder = gtk.Builder()
+
+        # Lendo o arquivo url_pyextractor.glade
+        builder.add_from_file(self.__class__.glade_file)
+
+        # Adicionando os objetos
+        self.janela = builder.get_object('janela')
+        self.in_url = builder.get_object('in_url')
+        self.bt_extrair = builder.get_object('bt_extrair')
+        self.bt_encurtar = builder.get_object('bt_encurtar')
+        self.status = builder.get_object('status')
+
+        # Conectando os sinais
+        builder.connect_signals(self)
+        
+        # Deixando o texto selecionado
+        self.in_url.select_region(0, -1)
+
+        # Iniciando o programa
+        self.iniciar()    
+        
+    def extrair(self, *args):
+        """
+        Este método é ativado quando o botão 'Extrair' é clicado.
+        Ele testa se a URL é válida, extrai o link verdadeiro e abre a página de download.
+        """
+        if self.in_url.get_text() == '':
+            self.url_vazia()
+        else:
+            url = Url(self.in_url.get_text())
+            if url.extrair() == -1:
+                self.url_invalida()    
+            else:
+                if url.eh_invertida():
+                    url.inverter()
+                self.in_url.set_text(url.extraida)
+                self.status.set_text('Sucesso! A página para download está sendo aberta.')
+                self.status.set_style(self.mudar_cor('green'))
+                wb.open(url.extraida)
+                
+
+    def encurtar(self, *args):
+        """
+        Este método é ativado quando o botão 'Encurtar' é clicado.
+        Ele verifica se a URL é válida, encurta o link e copia ele pra área de transferência.
+        """
+        if self.in_url.get_text() == '':
+            self.url_vazia()
+        elif self.in_url.get_text()[:7] != 'http://':
+            self.url_invalida()
+        else:
+            try:
+                url = Url(self.in_url.get_text())
+                url.encurtar()
+                self.in_url.set_text(url.curta)
+                self.copiar(self.in_url)
+            except IOError:
+                self.status.set_text('ERRO: Verifique sua conexão com a Internet.')
+                self.status.set_style(self.mudar_cor('red'))
+
+    def copiar(self, widget, *args):
+        """
+        Este método seleciona e copia para a área de transferência o texto que está em um GtkEntry.
+        """
+        widget.select_region(0, -1)
+        widget.copy_clipboard()
+        self.status.set_text('Sucesso! A URL foi copiada para a área de transferência.')
+        self.status.set_style(self.mudar_cor('green'))
+
+    def abrir_sobre(self, *args):
+        """
+        Método que constrói a janela 'Sobre'.
+        """
+        builder = gtk.Builder()
+        builder.add_from_file('gui/sobre.glade')
+        self.sobre = builder.get_object('sobre')
+        self.sobre.run()
+        self.sobre.destroy()
+
+    def esconder_sobre(self, widget, data):
+        self.sobre.hide()
+
+    def mudar_cor(self, cor):
+        texto = self.status.get_style().copy()
+        texto.fg[gtk.STATE_NORMAL] = self.status.get_colormap().alloc_color(cor)
+        return texto
+        
+    def url_invalida(self):
+        self.status.set_text('ERRO: URL inválida.')
+        self.status.set_style(self.mudar_cor('red'))
+        
+    def url_vazia(self):
+        self.status.set_text('ERRO: URL vazia.')
+        self.status.set_style(self.mudar_cor('red'))
+        
+    def iniciar(self):
+        self.janela.show()
+        gtk.main()
+
+    def sair(self, *args):
+        gtk.main_quit()
+
+
+class Url:            
+    def __init__(self, url):
+        self.longa = url
+
+    def eh_invertida(self):
+        if '//:ptth' in self.longa:
+            return True
+        else:
+            return False
+    
+    
+    def extrair(self):
+        if 'url=' in self.longa:
+            self.extraida = self.longa.split('url=')[1]
+        elif 'link=' in self.longa:
+            self.extraida = self.longa.split('link=')[1]
+        elif 'id=' in self.longa:
+            link_decodificado = self.decodificar(self.longa.split('id=')[1])
+            self.extraida = link_decodificado
+        else:
+            self.extraida = ''
+            return -1
+
+            
+    def inverter(self):
+        self.extraida = self.extraida[::-1]
+        
+        
+    def decodificar(self, codigo):
+	if len(codigo) > 1:
+		indice_inicial, indice_final = 0, 2
+		caracteres = []
+		while len(codigo) >= indice_final:
+			codigo_int = int(codigo[indice_inicial:indice_final], 16)
+			char_decodificado = chr(codigo_int)
+			caracteres.append(char_decodificado)
+			indice_inicial, indice_final = indice_final, indice_final + 2
+		return ''.join(caracteres)
+
+
+    def encurtar(self):
+        """
+        Método encurta urls por meio da API do bit.ly
+        
+        O código-fonte deste método é da autoria de José Lopes.
+        Ele foi retirado do post 'Usando a API do bit.ly com Python'
+        Fonte: http://joselopes.org/blog/usando-a-api-do-bit-ly-com-python/
+        """
+        
+        long_url_ = urllib.urlencode(dict(longUrl=self.longa))
+        api_user_ = urllib.urlencode(dict(login='luizaugustomm'))
+        api_key_ = urllib.urlencode(dict(apiKey='R_3e02d5a4d6f1924bab67e803de9235dd'))
+
+        encodedurl = 'http://api.bit.ly/shorten?\
+version=2.0.1&{0}&{1}&{2}'.format(long_url_, api_user_, api_key_)
+        request = urllib.urlopen(encodedurl)
+        responde = request.read()
+        request.close()
+        responde_dict = eval(responde)
+        short_url = responde_dict['results'][self.longa]['shortUrl']
+
+        self.curta = short_url
+
+
+if __name__ == '__main__':
+    url = Pyextractor()