Skip to content
Snippets Groups Projects
Commit 027fd63f authored by Dicson's avatar Dicson
Browse files

UrlShortenerPlugin.shortening links in outgoing message

parent 0c1b1395
No related branches found
No related tags found
No related merge requests found
......@@ -4,35 +4,58 @@
<!-- interface-naming-policy toplevel-contextual -->
<object class="GtkWindow" id="window1">
<child>
<object class="GtkHBox" id="hbox1">
<object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="extension_events">all</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="avatar_size_lebel">
<object class="GtkHBox" id="hbox1">
<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>
<property name="extension_events">all</property>
<child>
<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>
<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">
<object class="GtkCheckButton" id="shorten_outgoing">
<property name="label" translatable="yes">shorten links in outgoing messages</property>
<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"/>
<property name="receives_default">False</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
<signal name="toggled" handler="shorten_outgoing_toggled"/>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
......
[info]
name: Url Shortener
short_name: url_shortener
version: 0.1
description: Plugin that allows users to shorten a long URL in received messages.
version: 0.2
description: Plugin that allows users to shorten a long URL in messages.
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
Into this link:
......
......@@ -5,10 +5,14 @@ import json
import urllib
import urllib2
from common import gajim
from common import ged
from plugins import GajimPlugin
from plugins.helpers import log_calls
from plugins.gui import GajimPluginConfigDialog
APIKEY = 'R_fcba926fc7978bd19acbca73ec82b2be'
USER = 'dicson'
class UrlShortenerPlugin(GajimPlugin):
@log_calls('UrlShortenerPlugin')
def init(self):
......@@ -28,10 +32,42 @@ class UrlShortenerPlugin(GajimPlugin):
'print_special_text': (self.print_special_text,
self.print_special_text1),}
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.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')
def connect_with_chat_control(self, chat_control):
self.chat_control = chat_control
......@@ -59,8 +95,6 @@ class UrlShortenerPlugin(GajimPlugin):
class Base(object):
def __init__(self, plugin, chat_control):
self.user = 'dicson'
self.apikey = 'R_fcba926fc7978bd19acbca73ec82b2be'
self.plugin = plugin
self.chat_control = chat_control
self.textview = self.chat_control.conv_textview
......@@ -99,8 +133,8 @@ class Base(object):
def insert_hyperlink(self, mark, special_text, ttt):
try:
params = urllib.urlencode({'longUrl': special_text,
'login': self.user,
'apiKey': self.apikey,
'login': USER,
'apiKey': APIKEY,
'format': 'json'})
req = urllib2.Request('http://api.bit.ly/v3/shorten?%s' % params)
response = urllib2.urlopen(req)
......@@ -132,8 +166,8 @@ class Base(object):
if text.startswith('http://bit.ly/'):
try:
params = urllib.urlencode({'shortUrl': text,
'login': self.user,
'apiKey': self.apikey,
'login': USER,
'apiKey': APIKEY,
'format': 'json'})
req = urllib2.Request('http://api.bit.ly/v3/expand?%s' \
% params)
......@@ -161,17 +195,23 @@ class UrlShortenerPluginConfigDialog(GajimPluginConfigDialog):
'config_dialog.ui')
self.xml = gtk.Builder()
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.get_adjustment().set_all(30, 30, 99999, 1,
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.xml.connect_signals(self)
def on_run(self):
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):
self.plugin.config['MAX_CHARS'] = spinbutton.get_value()
def shorten_outgoing_toggled(self, checkbutton):
self.plugin.config['SHORTEN_OUTGOING'] = checkbutton.get_active()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment