bin env python coding utf-8 import urllib re ClientCookie sys ZODIAC Б

 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
#!/bin/env python
# -*- coding: utf-8 -*-
import urllib, re, ClientCookie, sys
ZODIAC = u"Близнецы"
DAYS = 3 # <=4
MAXSIZE = 126
USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1"
def fetch_page(zodiac):
opener = ClientCookie.build_opener(ClientCookie.HTTPCookieProcessor(ClientCookie.CookieJar()))
opener.addheaders = [("User-Agent", USER_AGENT)]
ClientCookie.install_opener(opener)
page = unicode(ClientCookie.urlopen('http://www.icqbot.org/web').read(), 'cp1251')
session_id = re.search(r'.*<input type="hidden" name="ibwiSession" value="(.*?)".*', page, re.S).group(1)
frame_uri = re.search(r'.*<iframe src="(.*?)".*', page, re.S).group(1)
page = ClientCookie.urlopen('http://www.icqbot.org%s' % frame_uri, urllib.urlencode({'ibwiInput' : 'horo %s' % zodiac, 'ibwiSession' : session_id})).read().decode('cp1251')
return page
def parse_page(page):
groups = re.search(r'.*~<br />\r\n' + r'<br />\r(\n.*?)<br />\r\n(.*?)<br />\r\n' * (DAYS-1) + r'<br />\r(\n.*?)<br />\r\n(.*?)<br />.*', page, re.S).groups()
s = ''
for i in groups:
s += i + '\n'
return s
def cut_page(page, maxsize):
cut = "%s:\n" % ZODIAC
i = 0
while i < len(page) - 1:
cut += "%s\n" % page[i:i+maxsize]
i += maxsize
cut += page[i:]
return cut
if __name__ == '__main__':
output = parse_page(fetch_page(ZODIAC.encode('cp1251')))
output = cut_page(output, MAXSIZE)
sys.stdout.write(output.encode('utf-8'))