UrlShortenerPlugin.shortening links in outgoing message

This commit is contained in:
Denis Fomin
2013-01-16 16:29:25 +04:00
parent 0c1b1395b4
commit 027fd63fd2
3 changed files with 90 additions and 27 deletions

View File

@@ -4,35 +4,58 @@
<!-- interface-naming-policy toplevel-contextual --> <!-- interface-naming-policy toplevel-contextual -->
<object class="GtkWindow" id="window1"> <object class="GtkWindow" id="window1">
<child> <child>
<object class="GtkHBox" id="hbox1"> <object class="GtkVBox" id="vbox1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="extension_events">all</property> <property name="orientation">vertical</property>
<child> <child>
<object class="GtkLabel" id="avatar_size_lebel"> <object class="GtkHBox" id="hbox1">
<property name="visible">True</property> <property name="visible">True</property>
<property name="xalign">0.029999999329447746</property> <property name="extension_events">all</property>
<property name="label" translatable="yes">The maximum length not be shortened links(chars)</property> <child>
<property name="track_visited_links">False</property> <object class="GtkLabel" id="avatar_size_lebel">
<property name="visible">True</property>
<property name="xalign">0.029999999329447746</property>
<property name="label" translatable="yes">The maximum length not be shortened links(chars)</property>
<property name="track_visited_links">False</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkSpinButton" id="max_chars">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property>
<property name="width_chars">6</property>
<property name="snap_to_ticks">True</property>
<property name="numeric">True</property>
<signal name="value_changed" handler="avatar_size_value_changed"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object> </object>
<packing> <packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property> <property name="position">0</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="max_chars"> <object class="GtkCheckButton" id="shorten_outgoing">
<property name="label" translatable="yes">shorten links in outgoing messages</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x25CF;</property> <property name="receives_default">False</property>
<property name="width_chars">6</property> <property name="active">True</property>
<property name="snap_to_ticks">True</property> <property name="draw_indicator">True</property>
<property name="numeric">True</property> <signal name="toggled" handler="shorten_outgoing_toggled"/>
<signal name="value_changed" handler="avatar_size_value_changed"/>
</object> </object>
<packing> <packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property> <property name="position">1</property>
</packing> </packing>
</child> </child>

View File

@@ -1,8 +1,8 @@
[info] [info]
name: Url Shortener name: Url Shortener
short_name: url_shortener short_name: url_shortener
version: 0.1 version: 0.2
description: Plugin that allows users to shorten a long URL in received messages. description: Plugin that allows users to shorten a long URL in messages.
For example, you can turn this link: For example, you can turn this link:
http://maps.google.com/maps?f=d&saddr=New+York+Penn+Station&daddr=9th+Ave+%26+14th+St,+New+York,+NY&hl=en&geocode=&mra=ls&dirflg=r&date=11%2F12%2F08&time=4:13pm&ttype=dep&noexp=0&noal=0&sort=&sll=40.746175,-73.998395&sspn=0.014468,0.036392&ie=UTF8&z=14 http://maps.google.com/maps?f=d&saddr=New+York+Penn+Station&daddr=9th+Ave+%26+14th+St,+New+York,+NY&hl=en&geocode=&mra=ls&dirflg=r&date=11%2F12%2F08&time=4:13pm&ttype=dep&noexp=0&noal=0&sort=&sll=40.746175,-73.998395&sspn=0.014468,0.036392&ie=UTF8&z=14
Into this link: Into this link:

View File

@@ -5,10 +5,14 @@ import json
import urllib import urllib
import urllib2 import urllib2
from common import gajim from common import gajim
from common import ged
from plugins import GajimPlugin from plugins import GajimPlugin
from plugins.helpers import log_calls from plugins.helpers import log_calls
from plugins.gui import GajimPluginConfigDialog from plugins.gui import GajimPluginConfigDialog
APIKEY = 'R_fcba926fc7978bd19acbca73ec82b2be'
USER = 'dicson'
class UrlShortenerPlugin(GajimPlugin): class UrlShortenerPlugin(GajimPlugin):
@log_calls('UrlShortenerPlugin') @log_calls('UrlShortenerPlugin')
def init(self): def init(self):
@@ -28,10 +32,42 @@ class UrlShortenerPlugin(GajimPlugin):
'print_special_text': (self.print_special_text, 'print_special_text': (self.print_special_text,
self.print_special_text1),} self.print_special_text1),}
self.config_default_values = { self.config_default_values = {
'MAX_CHARS': (50, _('MAX_CHARS(30-...)'))} 'MAX_CHARS': (50, _('MAX_CHARS(30-...)')),
'SHORTEN_OUTGOING': (False, ''),}
self.events_handlers['message-outgoing'] = (ged.OUT_PRECORE,
self.handle_outgoing_msg)
self.chat_control = None self.chat_control = None
self.controls = [] self.controls = []
def handle_outgoing_msg(self, event):
if not event.message:
return
if not self.config['SHORTEN_OUTGOING']:
return
iterator = gajim.interface.basic_pattern_re.finditer(event.message)
for match in iterator:
start, end = match.span()
link = event.message[start:end]
if len(link) < self.config['MAX_CHARS']:
continue
short_link = None
try:
params = urllib.urlencode({'longUrl': link,
'login': USER,
'apiKey': APIKEY,
'format': 'json'})
req = urllib2.Request('http://api.bit.ly/v3/shorten?%s' % params)
response = urllib2.urlopen(req)
j = json.load(response)
if j['status_code'] == 200:
short_link = j['data']['url']
except urllib2.HTTPError, e:
pass
if short_link:
event.message = event.message.replace(link, short_link)
event.callback_args[1] = event.message
@log_calls('UrlShortenerPlugin') @log_calls('UrlShortenerPlugin')
def connect_with_chat_control(self, chat_control): def connect_with_chat_control(self, chat_control):
self.chat_control = chat_control self.chat_control = chat_control
@@ -59,8 +95,6 @@ class UrlShortenerPlugin(GajimPlugin):
class Base(object): class Base(object):
def __init__(self, plugin, chat_control): def __init__(self, plugin, chat_control):
self.user = 'dicson'
self.apikey = 'R_fcba926fc7978bd19acbca73ec82b2be'
self.plugin = plugin self.plugin = plugin
self.chat_control = chat_control self.chat_control = chat_control
self.textview = self.chat_control.conv_textview self.textview = self.chat_control.conv_textview
@@ -99,8 +133,8 @@ class Base(object):
def insert_hyperlink(self, mark, special_text, ttt): def insert_hyperlink(self, mark, special_text, ttt):
try: try:
params = urllib.urlencode({'longUrl': special_text, params = urllib.urlencode({'longUrl': special_text,
'login': self.user, 'login': USER,
'apiKey': self.apikey, 'apiKey': APIKEY,
'format': 'json'}) 'format': 'json'})
req = urllib2.Request('http://api.bit.ly/v3/shorten?%s' % params) req = urllib2.Request('http://api.bit.ly/v3/shorten?%s' % params)
response = urllib2.urlopen(req) response = urllib2.urlopen(req)
@@ -132,8 +166,8 @@ class Base(object):
if text.startswith('http://bit.ly/'): if text.startswith('http://bit.ly/'):
try: try:
params = urllib.urlencode({'shortUrl': text, params = urllib.urlencode({'shortUrl': text,
'login': self.user, 'login': USER,
'apiKey': self.apikey, 'apiKey': APIKEY,
'format': 'json'}) 'format': 'json'})
req = urllib2.Request('http://api.bit.ly/v3/expand?%s' \ req = urllib2.Request('http://api.bit.ly/v3/expand?%s' \
% params) % params)
@@ -161,17 +195,23 @@ class UrlShortenerPluginConfigDialog(GajimPluginConfigDialog):
'config_dialog.ui') 'config_dialog.ui')
self.xml = gtk.Builder() self.xml = gtk.Builder()
self.xml.set_translation_domain('gajim_plugins') self.xml.set_translation_domain('gajim_plugins')
self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, ['hbox1']) self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH, ['vbox1'])
self.max_chars_spinbutton = self.xml.get_object('max_chars') self.max_chars_spinbutton = self.xml.get_object('max_chars')
self.max_chars_spinbutton.get_adjustment().set_all(30, 30, 99999, 1, self.max_chars_spinbutton.get_adjustment().set_all(30, 30, 99999, 1,
10, 0) 10, 0)
hbox = self.xml.get_object('hbox1') self.shorten_outgoing = self.xml.get_object('shorten_outgoing')
print self.shorten_outgoing
hbox = self.xml.get_object('vbox1')
self.child.pack_start(hbox) self.child.pack_start(hbox)
self.xml.connect_signals(self) self.xml.connect_signals(self)
def on_run(self): def on_run(self):
self.max_chars_spinbutton.set_value(self.plugin.config['MAX_CHARS']) self.max_chars_spinbutton.set_value(self.plugin.config['MAX_CHARS'])
self.shorten_outgoing.set_active(self.plugin.config['SHORTEN_OUTGOING'])
def avatar_size_value_changed(self, spinbutton): def avatar_size_value_changed(self, spinbutton):
self.plugin.config['MAX_CHARS'] = spinbutton.get_value() self.plugin.config['MAX_CHARS'] = spinbutton.get_value()
def shorten_outgoing_toggled(self, checkbutton):
self.plugin.config['SHORTEN_OUTGOING'] = checkbutton.get_active()