Thread: [pyKoL-users] SF.net SVN: pykol: [4]
Brought to you by:
scelis
From: <mi...@us...> - 2007-03-31 19:51:56
|
Revision: 4 http://pykol.svn.sourceforge.net/pykol/?rev=4&view=rev Author: misza13 Date: 2007-03-31 12:51:56 -0700 (Sat, 31 Mar 2007) Log Message: ----------- Encapsulating site-related methods into a single class. Modified Paths: -------------- login.py Added Paths: ----------- kolsite.py Added: kolsite.py =================================================================== --- kolsite.py (rev 0) +++ kolsite.py 2007-03-31 19:51:56 UTC (rev 4) @@ -0,0 +1,121 @@ +# -*- coding: utf-8 -*- + +import re, urllib, httplib + +try: + import hashlib + new_md5 = hashlib.md5 +except ImportError: #Old python? + import md5 + new_md5 = md5.md5 + + +def urlEncode(query): + """This can encode a query so that it can be sent as a query using + a http POST request""" + if not query: + return None + l = [] + for key, value in query.iteritems(): + key = urllib.quote(key) + value = urllib.quote(value) + l.append(key + '=' + value) + return '&'.join(l) + + +def challengePassword(challenge,password): + m = new_md5() + m.update(password) + hashedpw = m.hexdigest() + m = new_md5() + m.update(hashedpw+':'+challenge) + return m.hexdigest() + + +class LoginError: + def __init__(self,reason='None'): + self.reason = reason + + def __repr__(self): + return 'LoginError{%s}' % self.reason + + +class KoLSite: + def __init__(self, hostname): + self.hostname = hostname + self.cookie = '' + + + def getPage(self, address, data=''): + #print 'Getting %s...' % address + conn = httplib.HTTPConnection(self.hostname) + + conn.putrequest('GET', '/' + address) + #conn.putheader('Cookie', self.cookie) + conn.endheaders() + conn.send('') + + response = conn.getresponse() + data = response.read().decode('utf-8') + conn.close() + return response, data + + + def postForm(self, address, formdata): + data = urlEncode(formdata) + return self.postData(address, data) + + + def postData(self, address, data=''): + conn = httplib.HTTPConnection(self.hostname) + + conn.putrequest('POST', '/' + address) + conn.putheader('Content-Length', str(len(data))) + #conn.putheader('Content-type', 'application/x-www-form-urlencoded') + conn.endheaders() + conn.send(data) + + response = conn.getresponse() + data = response.read().decode('utf-8') + conn.close() + return response, data + + + def doLogin(self, nick, password): + challenge = self.getChallenge() + #print 'Challenge: "%s"' % challenge + + formFields = {} + formFields['loggingin'] = 'Yup.' + formFields['loginname'] = nick + formFields['challenge'] = challenge + formFields['response'] = challengePassword(challenge,password) + #formFields['password'] = password + formFields['secure'] = '1' + + print 'Now logging in...' + response, data = self.postForm('login.php',formFields) + + if response.status == 302: + self.cookie = response.getheader('set-cookie') + return + + #print response.status + if re.search('Bad password.',data): + raise LoginError, 'Bad password.' + elif re.search('Too many login failures from this IP.',data): + raise LoginError, 'Too many login failures from this IP.' + elif re.search('Too many login attempts',data): + raise LoginError, 'Too many login attempts.' + else: + raise LoginError, data + + + def getChallenge(self): + print 'Getting login challenge...' + response, data = self.getPage('login.php') + challenge = re.search('<input type=hidden name=challenge value="([0-9a-f]+)">',data) + if challenge: + return challenge.group(1) + else: + return '' Modified: login.py =================================================================== --- login.py 2007-03-31 19:04:25 UTC (rev 3) +++ login.py 2007-03-31 19:51:56 UTC (rev 4) @@ -1,134 +1,13 @@ # -*- coding: utf-8 -*- -import re -import urllib, httplib +from kolsite import KoLSite -try: - import hashlib - new_md5 = hashlib.md5 -except ImportError: #Old python? - import md5 - new_md5 = md5.md5 - -class LoginError: - def __init__(self,reason='None'): - self.reason = reason - - def __repr__(self): - return 'LoginError{%s}' % self.reason - - -def urlEncode(query): - """This can encode a query so that it can be sent as a query using - a http POST request""" - if not query: - return None - l = [] - for key, value in query.iteritems(): - key = urllib.quote(key) - value = urllib.quote(value) - l.append(key + '=' + value) - return '&'.join(l) - - -def postForm(hostname, address, predata): - data = urlEncode(predata) - return postData(hostname, address, data) - - -def postData(hostname, address, data, contentType = 'application/x-www-form-urlencoded'): - conn = httplib.HTTPConnection(hostname) - - conn.putrequest('POST', address) - conn.putheader('Content-Length', str(len(data))) - conn.putheader('Content-type', contentType) - conn.endheaders() - conn.send(data) - - response = conn.getresponse() - data = response.read().decode('utf-8') - conn.close() - return response, data - - -def getChallenge(hostname): - print 'Getting login challenge...' - txt = urllib.urlopen('http://'+hostname+'/login.php').read() - challenge = re.search('<input type=hidden name=challenge value="([0-9a-f]+)">',txt) - if challenge: - return challenge.group(1) - else: - return '' - - -def challengePassword(challenge,password): - m = new_md5() - m.update(password) - hashedpw = m.hexdigest() - m = new_md5() - m.update(hashedpw+':'+challenge) - return m.hexdigest() - - -def doLogin(hostname, nick, password): - challenge = getChallenge(hostname) - #print 'Challenge: "%s"' % challenge - - formFields = {} - formFields['loggingin'] = 'Yup.' - formFields['loginname'] = nick - formFields['challenge'] = challenge - formFields['response'] = challengePassword(challenge,password) - #formFields['password'] = password - formFields['secure'] = '1' - - print 'Now logging in...' - response, data = postForm(hostname,'/login.php',formFields) - - if response.status == 302: - return response.getheader('set-cookie') - - print response.status - if re.search('Bad password.',data): - raise LoginError, 'Bad password.' - elif re.search('Too many login failures from this IP.',data): - raise LoginError, 'Too many login failures from this IP.' - elif re.search('Too many login attempts',data): - raise LoginError, 'Too many login attempts.' - else: - print data - raise LoginError, '' - - -def getMain(hostname, cookie): - return getPage(hostname,'/main.html',cookie) - - -def getCharSheet(hostname, cookie): - return getPage(hostname,'/charsheet.php',cookie) - - -def getMain(hostname, page, cookie): - print 'Getting %s...' % page - conn = httplib.HTTPConnection(hostname) - - conn.putrequest('GET', page) - conn.putheader('Cookie', cookie) - conn.endheaders() - conn.send('') - - response = conn.getresponse() - data = response.read().decode('utf-8') - conn.close() - return response, data - - if __name__ == '__main__': - HOSTNAME = 'www.kingdomofloathing.com' + Site = KoLSite('www.kingdomofloathing.com') config = {} execfile('misha.conf') - cookie = doLogin(HOSTNAME,config['nick'],config['password']) - response, data = getMain(HOSTNAME,cookie) + cookie = Site.doLogin(config['nick'],config['password']) + response, data = Site.getPage('main.html') print response.status print data This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mi...@us...> - 2007-03-31 20:03:42
|
Revision: 5 http://pykol.svn.sourceforge.net/pykol/?rev=5&view=rev Author: misza13 Date: 2007-03-31 13:03:44 -0700 (Sat, 31 Mar 2007) Log Message: ----------- login.py -> test.py (more appropriate name) Modified Paths: -------------- kolsite.py Added Paths: ----------- test.py Removed Paths: ------------- login.py Modified: kolsite.py =================================================================== --- kolsite.py 2007-03-31 19:51:56 UTC (rev 4) +++ kolsite.py 2007-03-31 20:03:44 UTC (rev 5) @@ -47,11 +47,10 @@ def getPage(self, address, data=''): - #print 'Getting %s...' % address conn = httplib.HTTPConnection(self.hostname) conn.putrequest('GET', '/' + address) - #conn.putheader('Cookie', self.cookie) + conn.putheader('Cookie', self.cookie) conn.endheaders() conn.send('') @@ -71,7 +70,6 @@ conn.putrequest('POST', '/' + address) conn.putheader('Content-Length', str(len(data))) - #conn.putheader('Content-type', 'application/x-www-form-urlencoded') conn.endheaders() conn.send(data) @@ -83,7 +81,6 @@ def doLogin(self, nick, password): challenge = self.getChallenge() - #print 'Challenge: "%s"' % challenge formFields = {} formFields['loggingin'] = 'Yup.' Deleted: login.py =================================================================== --- login.py 2007-03-31 19:51:56 UTC (rev 4) +++ login.py 2007-03-31 20:03:44 UTC (rev 5) @@ -1,13 +0,0 @@ -# -*- coding: utf-8 -*- - -from kolsite import KoLSite - - -if __name__ == '__main__': - Site = KoLSite('www.kingdomofloathing.com') - config = {} - execfile('misha.conf') - cookie = Site.doLogin(config['nick'],config['password']) - response, data = Site.getPage('main.html') - print response.status - print data Copied: test.py (from rev 4, login.py) =================================================================== --- test.py (rev 0) +++ test.py 2007-03-31 20:03:44 UTC (rev 5) @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- + +import sys +from kolsite import KoLSite + + +def main(): + if len(sys.argv) < 2: + print 'Please specify a config file!' + return + config = {} + execfile('misha.conf') + if not config.has_key('nick') or not config.has_key('password'): + print 'Nick or password not specified in config!' + return + + Site = KoLSite('www.kingdomofloathing.com') + Site.doLogin(config['nick'],config['password']) + + response, data = Site.getPage('main.html') + print response.status + print data + +if __name__ == '__main__': + main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mi...@us...> - 2007-04-07 22:10:32
|
Revision: 13 http://pykol.svn.sourceforge.net/pykol/?rev=13&view=rev Author: misza13 Date: 2007-04-07 15:09:38 -0700 (Sat, 07 Apr 2007) Log Message: ----------- Modified the mall searcher into an iterator over the results (using yield). Modified Paths: -------------- mall.py test.py Modified: mall.py =================================================================== --- mall.py 2007-04-07 20:54:13 UTC (rev 12) +++ mall.py 2007-04-07 22:09:38 UTC (rev 13) @@ -11,10 +11,24 @@ def searchItem(self, what, limit=None): formdata = {'whichitem' : what} if limit: - formdata['cheaponly'] = 1 - formdata['shownum'] = limit + formdata['cheaponly'] = '1' + formdata['shownum'] = str(limit) response, data = self.Site.postForm('searchmall.php',formdata) - print response.status - print response.getheaders() - print data + offset = re.search('Price:',data).start() + itemRX = re.compile(r'<b>(?P<itemname>.+?)</b> \((?P<itemcount>\d+)\)(?: \((?P<limit>\d+) / day\))?.*?mallstore.php\?whichstore=(?P<whichstore>\d+).*?>(?P<shopname>.+)</a>.*?>(?P<price>[0-9,]+) ') + for itemM in itemRX.finditer(data,offset): + result = {} + for v in ['itemname','itemcount','limit','whichstore','shopname','price']: + if v == 'itemcount': + result[v] = int(itemM.group('itemcount').replace(',','')) + elif v == 'limit' and itemM.group('limit'): + result[v] = int(itemM.group('limit').replace(',','')) + elif v == 'price': + result[v] = int(itemM.group('price').replace(',','')) + elif v == 'whichstore': + result[v] = int(itemM.group('whichstore').replace(',','')) + else: + result[v] = itemM.group(v) + yield result + Modified: test.py =================================================================== --- test.py 2007-04-07 20:54:13 UTC (rev 12) +++ test.py 2007-04-07 22:09:38 UTC (rev 13) @@ -18,12 +18,13 @@ Site = KoLSite() Site.doLogin(config['nick'],config['password']) - response, data = Site.getPage('main.html') - print response.status - print data + #response, data = Site.getPage('main.html') + #print response.status + #print data M = Mall(Site) - M.searchItem('tiny plastic sword') + for s in M.searchItem('tiny plastic sword'): + print s if __name__ == '__main__': main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <mi...@us...> - 2007-04-29 19:45:05
|
Revision: 15 http://pykol.svn.sourceforge.net/pykol/?rev=15&view=rev Author: misza13 Date: 2007-04-29 12:45:04 -0700 (Sun, 29 Apr 2007) Log Message: ----------- Adding licensing information. Modified Paths: -------------- kolsite.py mall.py test.py Added Paths: ----------- LICENSE Added: LICENSE =================================================================== --- LICENSE (rev 0) +++ LICENSE 2007-04-29 19:45:04 UTC (rev 15) @@ -0,0 +1,19 @@ +Copyright (C) Misza <mi...@mi...>, 2007 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. Modified: kolsite.py =================================================================== --- kolsite.py 2007-04-08 21:58:49 UTC (rev 14) +++ kolsite.py 2007-04-29 19:45:04 UTC (rev 15) @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- - +# +# (C) Misza <mi...@mi...>, 2007 +# +# Distributed under the terms of the MIT license. +# import re, urllib, httplib try: Modified: mall.py =================================================================== --- mall.py 2007-04-08 21:58:49 UTC (rev 14) +++ mall.py 2007-04-29 19:45:04 UTC (rev 15) @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- - +# +# (C) Misza <mi...@mi...>, 2007 +# +# Distributed under the terms of the MIT license. +# import re import kolsite Modified: test.py =================================================================== --- test.py 2007-04-08 21:58:49 UTC (rev 14) +++ test.py 2007-04-29 19:45:04 UTC (rev 15) @@ -1,5 +1,9 @@ # -*- coding: utf-8 -*- - +# +# (C) Misza <mi...@mi...>, 2007 +# +# Distributed under the terms of the MIT license. +# import sys, random from kolsite import KoLSite from mall import Mall This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |