usr bin env python from optparse import OptionParser from grab import

 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
from optparse import OptionParser
from grab import Grab
import logging
import sys
LI_CODE = """<!--LiveInternet counter--><script type="text/javascript">
document.write("&lt;a href='http://www.liveinternet.ru/click;%(nick)s' target='_blank'&gt;&lt;img src='http://counter.yadro.ru/hit;%(nick)s?t44.1;r"+escape(document.referrer)+((typeof(screen)=="undefined")?"":";s"+screen.width+"*"+screen.height+"*"+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+";u"+escape(document.URL)+";"+Math.random()+"' alt='' "+"border='0' width='31' height='31'&gt;&lt;\/a&gt;")</script>
<!--/LiveInternet-->"""
logging.basicConfig(level=logging.NOTSET)
usage = """Usage:
%prog -n <nick> [-e <email>] [-p <password>] - to create new account
%prog -c -n <nick> - to show counter code
"""
parser = OptionParser(usage=usage)
parser.add_option('-p', '--password', default='123ewq', help='Password')
parser.add_option('-e', '--email', default='lorien@lorien.name', help='Email')
parser.add_option('-n', '--nick', help='Account name')
parser.add_option('-c', '--code', action='store_true', default=False, help='Show the counter code')
kwargs, args = parser.parse_args()
if not kwargs.nick:
parser.error('Account name required.')
if kwargs.code:
print LI_CODE % {'nick': kwargs.nick}
sys.exit()
g = Grab()
g.setup(log_dir='var', unicode_body=False)
g.setup(url='http://www.liveinternet.ru/add')
g.request()
post = {
'rules': 'agreed',
'type': 'account',
'random': g.input_value('random'),
'nick': kwargs.nick,
'url': 'http://domain.com',
'name': kwargs.nick,
'email': kwargs.email,
'password': kwargs.password,
'check': kwargs.password,
'private': 'on',
'language': 'ru',
'confirmed': 'OK',
}
g.setup(url='http://www.liveinternet.ru/add', post=post)
g.request()
errors = []
for b in g.soup.findAll('b'):
if 'Error' in unicode(b):
errors.append(b.contents[0])
if errors:
for error in errors:
logging.error(error)
else:
if 'Is successfully registered' in g.response_body:
logging.info('Done. Nick: %s. Email: %s' % (kwargs.nick, kwargs.email))
else:
raise Exception('Success message have not been found')