Merge lp:~stefanor/ibid/faves-url into lp:~ibid-core/ibid/old-trunk-1.6

Proposed by Stefano Rivera
Status: Superseded
Proposed branch: lp:~stefanor/ibid/faves-url
Merge into: lp:~ibid-core/ibid/old-trunk-1.6
Diff against target: 205 lines (+81/-66)
2 files modified
ibid.ini (+4/-3)
ibid/plugins/urlgrab.py (+77/-63)
To merge this branch: bzr merge lp:~stefanor/ibid/faves-url
Reviewer Review Type Date Requested Status
Ibid Core Team Pending
Review via email: mp+17606@code.launchpad.net

This proposal has been superseded by a proposal from 2010-01-18.

To post a comment you must log in.
Revision history for this message
Stefano Rivera (stefanor) wrote :

Stop gap solution

lp:~stefanor/ibid/faves-url updated
848. By Stefano Rivera

Merge from trunk

849. By Stefano Rivera

Fix Merge errors

850. By Stefano Rivera

Remove urlgrab_ prefix

851. By Stefano Rivera

Remove unused import

852. By Stefano Rivera

Whoops, debugging code left behind

853. By Stefano Rivera

Remove leading space

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ibid.ini'
2--- ibid.ini 2010-01-04 12:23:00 +0000
3+++ ibid.ini 2010-01-18 21:57:14 +0000
4@@ -77,9 +77,10 @@
5 source = atrum
6 channel = "#ibid"
7 server = localhost
8- [[url]]
9- delicious_username = ibidtest
10- delicious_password = a123456
11+ [[urlgrab]]
12+ urlgrab_username = ibidtest
13+ urlgrab_password = a123456
14+ urlgrab_service = delicious
15
16 [databases]
17 ibid = sqlite:///ibid.db
18
19=== modified file 'ibid/plugins/urlgrab.py'
20--- ibid/plugins/urlgrab.py 2010-01-16 18:13:23 +0000
21+++ ibid/plugins/urlgrab.py 2010-01-18 21:57:14 +0000
22@@ -1,8 +1,7 @@
23 from datetime import datetime
24 from httplib import BadStatusLine
25 from urllib import urlencode
26-from urllib2 import urlopen, build_opener, HTTPError, HTTPBasicAuthHandler, \
27- install_opener
28+from urllib2 import urlopen, build_opener, HTTPError, HTTPBasicAuthHandler
29 import logging
30 import re
31
32@@ -19,7 +18,8 @@
33
34 log = logging.getLogger('plugins.urlgrab')
35
36-help['url'] = u'Captures URLs seen in channel to database and/or to delicious, and shortens and lengthens URLs'
37+help['url'] = u'Captures URLs seen in channel to database and/or to ' \
38+ u'delicious/faves'
39
40 class URL(Base):
41 __table__ = Table('urls', Base.metadata,
42@@ -48,8 +48,49 @@
43 self.identity_id = identity_id
44 self.time = datetime.utcnow()
45
46-class Delicious(object):
47- def add_post(self, username, password, event, url=None):
48+class Grab(Processor):
49+ addressed = False
50+ processed = True
51+
52+ username = Option('urlgrab_username', 'Account name for URL posting')
53+ password = Option('urlgrab_password', 'Password for URL Posting')
54+ service = Option('urlgrab_service',
55+ 'URL Posting Service (delicious/faves)', 'delicious')
56+
57+ def setup(self):
58+ if resource_exists(__name__, '../data/tlds-alpha-by-domain.txt'):
59+ tlds = [tld.strip().lower() for tld
60+ in resource_stream(__name__, '../data/tlds-alpha-by-domain.txt')
61+ .readlines()
62+ if not tld.startswith('#')
63+ ]
64+
65+ else:
66+ log.warning(u"Couldn't open TLD list, falling back to minimal default")
67+ tlds = 'com.org.net.za'.split('.')
68+
69+ self.grab.im_func.pattern = re.compile((
70+ r'(?:[^@./]\b(?!\.)|\A)(' # Match a boundary, but not on an e-mail address
71+ r'(?:\w+://|(?:www|ftp)\.)\S+?' # Match an explicit URL or guess by www.
72+ r'|[^@\s:/]+\.(?:%s)(?:/\S*?)?' # Guess at the URL based on TLD
73+ r')[\[>)\]"\'.,;:]*(?:\s|\Z)' # End boundary
74+ ) % '|'.join(tlds), re.I | re.DOTALL)
75+
76+ @handler
77+ def grab(self, event, url):
78+ if url.find('://') == -1:
79+ if url.lower().startswith('ftp'):
80+ url = 'ftp://%s' % url
81+ else:
82+ url = 'http://%s' % url
83+
84+ u = URL(url, event.channel, event.identity)
85+ event.session.save_or_update(u)
86+
87+ if self.service and self.username:
88+ self._post_url(event, url)
89+
90+ def _post_url(self, event, url=None):
91 "Posts a URL to delicious.com"
92
93 date = datetime.utcnow()
94@@ -75,7 +116,8 @@
95 obfusc_conn = at_re.sub('^', connection_body[1])
96 obfusc_chan = at_re.sub('^', event.channel)
97
98- tags = u' '.join((event.sender['nick'], obfusc_conn, obfusc_chan, event.source))
99+ tags = u' '.join((event.sender['nick'], obfusc_conn, obfusc_chan,
100+ event.source))
101
102 data = {
103 'url' : url.encode('utf-8'),
104@@ -86,18 +128,39 @@
105 'extended' : event.message['raw'].encode('utf-8'),
106 }
107
108- self._set_auth(username, password)
109- posturl = 'https://api.del.icio.us/v1/posts/add?' + urlencode(data)
110+ if self.service.lower() == 'delicious':
111+ service = ('del.icio.us API', 'https://api.del.icio.us')
112+ elif self.service.lower() == 'faves':
113+ service = ('Faves', 'https://secure.faves.com')
114+ else:
115+ log.error(u'Unknown social bookmarking service: %s', self.service)
116+ return
117+ auth_handler = HTTPBasicAuthHandler()
118+ auth_handler.add_password(service[0], service[1],
119+ self.username, self.password)
120+ opener = build_opener(auth_handler)
121+
122+ posturl = service[1] + '/v1/posts/add?' + urlencode(data)
123
124 try:
125- resp = urlopen(posturl).read()
126+ resp = opener.open(posturl).read()
127 if 'done' in resp:
128- log.debug(u"Posted url '%s' to delicious, posted in %s on %s by %s/%i (%s)",
129- url, event.channel, event.source, event.account, event.identity, event.sender['connection'])
130+ log.debug(u"Posted url '%s' to %s, posted in %s on %s "
131+ u"by %s/%i (%s)",
132+ url, self.service, event.channel, event.source,
133+ event.account, event.identity,
134+ event.sender['connection'])
135 else:
136- log.error(u"Error posting url '%s' to delicious: %s", url, resp)
137+ log.error(u"Error posting url '%s' to %s: %s",
138+ url, self.service, resp)
139+ except HTTPError, e:
140+ if e.code == 401:
141+ log.error(u"Incorrect password for %s, couldn't post",
142+ self.service)
143+ print e.hdrs
144 except BadStatusLine, e:
145- log.error(u"Error posting url '%s' to delicious: %s", url, unicode(e))
146+ log.error(u"Error posting url '%s' to %s: %s",
147+ url, self.service, unicode(e))
148
149 def _get_title(self, url):
150 "Gets the title of a page"
151@@ -112,53 +175,4 @@
152 log.debug(u"Error determining title for %s: %s", url, unicode(e))
153 return url
154
155- def _set_auth(self, username, password):
156- "Provides HTTP authentication on username and password"
157- auth_handler = HTTPBasicAuthHandler()
158- auth_handler.add_password('del.icio.us API', 'https://api.del.icio.us', username, password)
159- opener = build_opener(auth_handler)
160- install_opener(opener)
161-
162-class Grab(Processor):
163- addressed = False
164- processed = True
165-
166- username = Option('delicious_username', 'delicious account name')
167- password = Option('delicious_password', 'delicious account password')
168- delicious = Delicious()
169-
170- def setup(self):
171- if resource_exists(__name__, '../data/tlds-alpha-by-domain.txt'):
172- tlds = [tld.strip().lower() for tld
173- in resource_stream(__name__, '../data/tlds-alpha-by-domain.txt')
174- .readlines()
175- if not tld.startswith('#')
176- ]
177-
178- else:
179- log.warning(u"Couldn't open TLD list, falling back to minimal default")
180- tlds = 'com.org.net.za'.split('.')
181-
182- self.grab.im_func.pattern = re.compile((
183- r'(?:[^@./]\b(?!\.)|\A)(' # Match a boundary, but not on an e-mail address
184- r'(?:\w+://|(?:www|ftp)\.)\S+?' # Match an explicit URL or guess by www.
185- r'|[^@\s:/]+\.(?:%s)(?:/\S*?)?' # Guess at the URL based on TLD
186- r')[\[>)\]"\'.,;:]*(?:\s|\Z)' # End boundary
187- ) % '|'.join(tlds), re.I | re.DOTALL)
188-
189- @handler
190- def grab(self, event, url):
191- if url.find('://') == -1:
192- if url.lower().startswith('ftp'):
193- url = 'ftp://%s' % url
194- else:
195- url = 'http://%s' % url
196-
197- u = URL(url, event.channel, event.identity)
198- event.session.save_or_update(u)
199-
200- if self.username != None:
201- self.delicious.add_post(self.username, self.password, event, url)
202-
203-
204-# vi: set et sta sw=4 ts=4:
205+ # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches