-
Notifications
You must be signed in to change notification settings - Fork 0
/
indafoto.py
81 lines (71 loc) · 2.56 KB
/
indafoto.py
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
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Indafoto.hu manipulation library
"""
import re, os, urllib, urllib2, cookielib
import wikipedia as pywikibot
class Session:
noEmailAddressRegexp = re.compile(r'Ez a felhasználó sajnos nem adott meg értesítési\s+címet így neki nem tudsz üzenetet küldeni.')
csrfRegexp = re.compile(r'<input type="hidden" name="csrf_token" value="([\d\w-]+)" />')
sendSuccessRegexp = re.compile(r'<p>Sikeresen elküldve\.</p>')
def __init__(self, user = None):
self.user = user
policy = cookielib.DefaultCookiePolicy(allowed_domains = ['indapass.hu', 'indafoto.hu', '.indapass.hu', '.indafoto.hu'])
self.cookiejar = cookielib.LWPCookieJar(policy = policy)
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar))
if self.user:
cookie_file = self.getLoginDataFile()
if os.path.exists(cookie_file):
self.cookiejar.load(cookie_file, ignore_discard = True, ignore_expires = True)
def decorateFileRead(self, f):
content = f.read()
f.read = lambda: content
return f
def getLoginDataFile(self):
if not self.user:
raise IndafotoException('getLoginDataFile() with no user given')
return 'login-data/indafoto-%s-login.data' % self.user
def login(self):
url = 'https://daemon.indapass.hu/http/login'
if self.user:
pywikibot.output(u'Logging in as Inda user %s...' % self.user)
else:
pywikibot.output(u'Logging in to Inda...')
self.user = pywikibot.input('Email: ')
password = pywikibot.input('Password: ', password = True)
data = urllib.urlencode({
'partner_id': 'indapass',
'username': self.user,
'password': password,
'autologin': '1',
})
self.opener.open(url, data)
self.cookiejar.save(self.getLoginDataFile(), ignore_discard = True, ignore_expires = True)
def getCsrfToken(self, response):
m = self.csrfRegexp.search(response.read())
if not m:
raise NotLoggedInException()
return m.group(1)
def sendMessage(self, user, message):
url = 'http://indafoto.hu/%s/details' % user
f = self.decorateFileRead(self.opener.open(url))
if self.noEmailAddressRegexp.search(f.read()):
return False
try:
csrf_token = self.getCsrfToken(f)
except NotLoggedInException:
self.login()
f = self.opener.open(url)
csrf_token = self.getCsrfToken(f)
data = urllib.urlencode({
'csrf_token': csrf_token,
'body': message.encode('utf-8'),
'submit': 'Mehet',
})
f = self.opener.open(url + '/send', data)
return bool(self.sendSuccessRegexp.search(f.read()))
class IndafotoException(Exception):
pass
class NotLoggedInException(IndafotoException):
pass