~stefanor/ibid/apt-1020751

« back to all changes in this revision

Viewing changes to ibid/plugins/feeds.py

  • Committer: Tarmac
  • Author(s): Jonathan Hitchcock
  • Date: 2011-10-31 13:40:57 UTC
  • mfrom: (1027.2.2 feeds-747635)
  • Revision ID: ibid-lp-lander@rivera.za.net-20111031134057-zirigx0j4517gzan
Make feeds syntax more intuitive/responsive.
Author: Jonathan Hitchcock
Merge Request: http://code.launchpad.net/~vhata/ibid/feeds-747635/+merge/80198
Approved by: Keegan Carruthers-Smith, Stefano Rivera
Fixes LP: #747635

Show diffs side-by-side

added added

removed removed

Lines of Context:
225
225
broken_lock = Lock()
226
226
 
227
227
class Retrieve(Processor):
228
 
    usage = u"""latest [ <count> ] articles from <name> [ starting at <number> ]
 
228
    usage = u"""latest [ <count> ] ( articles | headlines ) from <name> [ starting at <number> ]
229
229
    article ( <number> | /<pattern>/ ) from <name>"""
230
230
    features = ('feeds',)
231
231
 
236
236
        'The slowdown ratio to back off from broken feeds', 2.0)
237
237
 
238
238
 
239
 
    @match(r'^(?:latest|last)\s+(?:(\d+)\s+)?articles\s+from\s+(.+?)'
 
239
    @match(r'^(?:latest|last)\s+(?:(\d+)\s+)?(article|headline)(s)?\s+from\s+(.+?)'
240
240
           r'(?:\s+start(?:ing)?\s+(?:at\s+|from\s+)?(\d+))?$')
241
 
    def list(self, event, number, name, start):
242
 
        number = number and int(number) or 10
 
241
    def list(self, event, number, full, plurality, name, start):
 
242
        full = full == 'article'
 
243
        if number:
 
244
            number = int(number)
 
245
        elif not plurality:
 
246
            number = 1
 
247
        else:
 
248
            number = 10
243
249
        start = start and int(start) or 0
244
250
 
245
251
        feed = event.session.query(Feed).filter_by(name=name).first()
254
260
            return
255
261
 
256
262
        articles = feed.entries[start:number+start]
257
 
        articles = [u'%s: "%s"' % (feed.entries.index(entry) + 1,
258
 
                                   html2text_file(entry.title, None).strip())
259
 
                    for entry in articles]
260
 
        event.addresponse(u', '.join(articles))
 
263
        entries = []
 
264
        for article in articles:
 
265
            if full:
 
266
                if 'summary' in article:
 
267
                    summary = html2text_file(article.summary, None)
 
268
                else:
 
269
                    if article.content[0].type in \
 
270
                            ('application/xhtml+xml', 'text/html'):
 
271
                        summary = html2text_file(article.content[0].value, None)
 
272
                    else:
 
273
                        summary = article.content[0].value
 
274
 
 
275
                entries.append(u'%(number)s: "%(title)s"%(link)s : %(summary)s' % {
 
276
                    'number': articles.index(article) + 1,
 
277
                    'title': html2text_file(article.title, None).strip(),
 
278
                    'link': get_link(article),
 
279
                    'summary': summary,
 
280
                })
 
281
            else:
 
282
                entries.append(u'%s: "%s"' % (feed.entries.index(article) + 1, html2text_file(article.title, None).strip()))
 
283
        event.addresponse(u', '.join(entries))
261
284
 
262
285
    @match(r'^article\s+(?:(\d+)|/(.+?)/)\s+from\s+(.+?)$')
263
286
    def article(self, event, number, pattern, name):
269
292
 
270
293
        feed.update()
271
294
        if not feed.entries:
272
 
            event.addresponse(u"I can't access that feed")
 
295
            event.addresponse(u"I can't find any articles in that feed")
273
296
            return
274
297
        article = None
275
298