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

Proposed by Stefano Rivera
Status: Merged
Approved by: Michael Gorven
Approved revision: 853
Merged at revision: 859
Proposed branch: lp:~stefanor/ibid/faves-url
Merge into: lp:~ibid-core/ibid/old-trunk-1.6
Diff against target: 203 lines (+79/-65)
2 files modified
ibid.ini (+4/-3)
ibid/plugins/urlgrab.py (+75/-62)
To merge this branch: bzr merge lp:~stefanor/ibid/faves-url
Reviewer Review Type Date Requested Status
Michael Gorven Approve
Jonathan Hitchcock Approve
marcog (community) Approve
Review via email: mp+17632@code.launchpad.net

This proposal supersedes a proposal from 2010-01-18.

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

Stop gap solution

Revision history for this message
Stefano Rivera (stefanor) wrote :

Should be noted that this doesn't seem to get on with https_proxy. They both use custom openers...

Revision history for this message
marcog (marco-gallotta) wrote :

+ # vi: set et sta sw=4 ts=4:

Leading space?

Otherwise, looks good.

review: Approve
lp:~stefanor/ibid/faves-url updated
853. By Stefano Rivera

Remove leading space

Revision history for this message
Stefano Rivera (stefanor) wrote :

> + # vi: set et sta sw=4 ts=4:

r853

Revision history for this message
Jonathan Hitchcock (vhata) :
review: Approve
Revision history for this message
Michael Gorven (mgorven) wrote :

 review approve
 status approved

review: Approve

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-24 16:40:26 +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+ username = ibidtest
13+ password = a123456
14+ 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-18 23:20:33 +0000
21+++ ibid/plugins/urlgrab.py 2010-01-24 16:40:26 +0000
22@@ -4,8 +4,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 build_opener, HTTPError, HTTPBasicAuthHandler
29 import logging
30 import re
31
32@@ -22,7 +21,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@@ -51,8 +51,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('username', 'Account name for URL posting')
53+ password = Option('password', 'Password for URL Posting')
54+ service = Option('service', 'URL Posting Service (delicious/faves)',
55+ '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@@ -78,7 +119,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@@ -89,18 +131,38 @@
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 except BadStatusLine, e:
144- log.error(u"Error posting url '%s' to delicious: %s", url, unicode(e))
145+ log.error(u"Error posting url '%s' to %s: %s",
146+ url, self.service, unicode(e))
147
148 def _get_title(self, url):
149 "Gets the title of a page"
150@@ -115,53 +177,4 @@
151 log.debug(u"Error determining title for %s: %s", url, unicode(e))
152 return url
153
154- def _set_auth(self, username, password):
155- "Provides HTTP authentication on username and password"
156- auth_handler = HTTPBasicAuthHandler()
157- auth_handler.add_password('del.icio.us API', 'https://api.del.icio.us', username, password)
158- opener = build_opener(auth_handler)
159- install_opener(opener)
160-
161-class Grab(Processor):
162- addressed = False
163- processed = True
164-
165- username = Option('delicious_username', 'delicious account name')
166- password = Option('delicious_password', 'delicious account password')
167- delicious = Delicious()
168-
169- def setup(self):
170- if resource_exists(__name__, '../data/tlds-alpha-by-domain.txt'):
171- tlds = [tld.strip().lower() for tld
172- in resource_stream(__name__, '../data/tlds-alpha-by-domain.txt')
173- .readlines()
174- if not tld.startswith('#')
175- ]
176-
177- else:
178- log.warning(u"Couldn't open TLD list, falling back to minimal default")
179- tlds = 'com.org.net.za'.split('.')
180-
181- self.grab.im_func.pattern = re.compile((
182- r'(?:[^@./]\b(?!\.)|\A)(' # Match a boundary, but not on an e-mail address
183- r'(?:\w+://|(?:www|ftp)\.)\S+?' # Match an explicit URL or guess by www.
184- r'|[^@\s:/]+\.(?:%s)(?:/\S*?)?' # Guess at the URL based on TLD
185- r')[\[>)\]"\'.,;:]*(?:\s|\Z)' # End boundary
186- ) % '|'.join(tlds), re.I | re.DOTALL)
187-
188- @handler
189- def grab(self, event, url):
190- if url.find('://') == -1:
191- if url.lower().startswith('ftp'):
192- url = 'ftp://%s' % url
193- else:
194- url = 'http://%s' % url
195-
196- u = URL(url, event.channel, event.identity)
197- event.session.save_or_update(u)
198-
199- if self.username != None:
200- self.delicious.add_post(self.username, self.password, event, url)
201-
202-
203 # vi: set et sta sw=4 ts=4:

Subscribers

People subscribed via source and target branches