coding utf-8 File welinux py Author Sergei Lebedev superbobry gmail co

 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
45
# -*- coding: utf-8 -*-
"""
File : welinux.py
Author : Sergei Lebedev <superbobry@gmail.com>
Created : Втр, Сен 22 2009 by Sergei Lebedev
Description : Welinux parser
"""
from urllib2 import urlopen
from re import finditer
RE_USER_DATA = r"(?is)" \
"(?:<span\sid='ru%(nick)s'><span\sclass='rp'>(?P<karma>[^<]+)" \
"|user\/city\/(?P<city>[^']+)" \
"|xmpp:(?P<jid>[^']+)" \
"|noindex><a\shref='(?P<url>[^']+)" \
"|auth\/%(nick)s[^>]+>(?P<posts>\d+)" \
"|comment\/%(nick)s[^>]+>(?P<comments>\d+))"
def user_data_generator(nick):
"""
Generator function, parses out user data with a given nick
and yields the fields one by one.
"""
url = "http://welinux.ru/user/%s" % nick
body = urlopen(url).read()
for match in finditer(RE_USER_DATA % locals(), body):
data = filter(
lambda kv: kv[1], match.groupdict().iteritems())
if data:
yield data[0]
def get_user_data(nick):
"""
Shortcut function, returns a data dict for a given nick.
"""
return dict(user_data_generator(nick))
print get_user_data('bobry')