import feedparser import re import urllib re_tag re compile re def com

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import feedparser
import re
import urllib
re_tag = re.compile(u'<[^>]+>', re.S)
def command(arg):
"""
Search on blogs.yandex.ru
"""
return search(arg)
def strip_tags(text):
"""
Remove tags.
"""
return re_tag.sub('', text)
def search(query):
"""
Parse query feed from blogs.yandex.ru and return data from first result.
"""
url = 'http://blogs.yandex.ru/search.rss?text=%s&ft=blog'
url %= urllib.quote(query.encode('utf-8'))
feed = feedparser.parse(url)
if not feed.entries:
return 'No results'
else:
entry = feed.entries[0]
title = entry.title
summary = strip_tags(entry.summary)
link = entry.link
return u'\n*%s*\n%s\n%s' % (title, summary, link)
if __name__ == '__main__':
import sys
print search(sys.argv[1].decode('utf-8'))