Created
September 27, 2012 08:01
-
-
Save y-ogi/3792790 to your computer and use it in GitHub Desktop.
Python script that can get the dartslive data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import logging | |
import re | |
import mechanize | |
import yaml | |
from BeautifulSoup import BeautifulSoup | |
class LoginFailException(Exception): | |
pass | |
class LoginRequiredException(Exception): | |
pass | |
class OpenFailException(Exception): | |
pass | |
class Group(object): | |
def __init__(self, gid, name): | |
self.gid = gid | |
self.name = name | |
class Member(object): | |
def __init__(self, mid, name, torina=None, rating=None): | |
self.mid = mid | |
self.name = name | |
self.torina = torina | |
self.rating = rating | |
class DartsLive(object): | |
LOGIN_URL = 'http://card.dartslive.com/t/login.jsp' | |
LOGIN_TITLE = 'ログイン' | |
GROUP_URL = 'http://card.dartslive.com/t/group/index.jsp' | |
GROUP_TITLE = 'グループ' | |
MEMBERS_URL = 'http://card.dartslive.com/t/group/top.jsp?n=%(gid)s&top=0' | |
MEMBERS_TITLE = '' | |
MEMBER_URL = 'http://card.dartslive.com/t/profile/index.jsp?u=%(mid)s' | |
MEMBER_TITLE = '' | |
def __init__(self, username, password): | |
self.username = username | |
self.password = password | |
self.br = mechanize.Browser() | |
self.is_loggined = False | |
def login(self): | |
try: | |
# open login url | |
self.br.set_handle_robots(False) | |
self.br.open(self.LOGIN_URL) | |
# select form and put the username and password | |
self.br.select_form(nr=0) | |
self.br['i'] = self.username | |
self.br['p'] = self.password | |
# get the html | |
res = self.br.submit() | |
html = res.read() | |
res.close() | |
logging.debug(unicode(html, self.br.encoding()).encode('utf-8')) | |
# change the flag | |
self.is_loggined = True | |
if self.LOGIN_TITLE in self.br.title(): | |
raise LoginFailException | |
except: | |
raise LoginFailException | |
return True | |
def groups(self): | |
if not self.is_loggined: | |
raise LoginRequiredException | |
# open groups url | |
res = self.br.open(self.GROUP_URL) | |
if not self.br.title() == self.GROUP_TITLE: | |
raise OpenFailException | |
# get the html and get the groups list | |
html = res.read() | |
soup = BeautifulSoup(html) | |
links = soup.findAll('a', {'class': 'groupName'}) | |
groups = [] | |
for link in links: | |
try: | |
href = link['href'] | |
gid = re.compile('n=([0-9]+)').search(href).group(1) | |
name = link.next | |
if name == 'NEW GROUP': continue | |
groups.append(Group(gid, name)) | |
except: | |
pass | |
return groups | |
def members(self, gid): | |
if not self.is_loggined: | |
raise LoginFailException | |
# open group top url | |
res = self.br.open(self.MEMBERS_URL % (locals())) | |
# get the html and get the member list | |
html = res.read() | |
soup = BeautifulSoup(html) | |
spans = soup.findAll('span', {'class': 'memberName'}) | |
members = [] | |
for span in spans: | |
try: | |
# name | |
name = re.sub(r'[^;]+?;', '', span.text) | |
# member id | |
href = span.parent.parent.findAll('a')[0]['href'] | |
mid = re.compile('i=([0-9]+)').search(href).group(1) | |
members.append(Member(mid, name)) | |
except: | |
pass | |
return members | |
def member(self, mid=None): | |
if not self.is_loggined: | |
raise LoginFailException | |
# open group top url | |
mid = mid if mid else self.username | |
res = self.br.open(self.MEMBER_URL % (locals())) | |
member = None | |
try: | |
# get the html and get the member list | |
html = res.read() | |
soup = BeautifulSoup(html) | |
# get the player name | |
h2s = soup.findAll('h2', {'class': 'playerName'}) | |
name = h2s[0].text | |
# get the trina | |
ps = soup.findAll('p', {'class': 'torina'}) | |
torina = ps[0].text | |
# get the rating | |
spans = soup.findAll('span', {'id': 'refValue'}) | |
rating = float(spans[0].text) | |
member = Member(mid, name, torina, rating) | |
except: | |
pass | |
return member | |
if __name__ == '__main__': | |
# load from the yaml | |
user = yaml.load(open('.dartslive.yaml').read()) | |
# test | |
dl = DartsLive(user['cardid'], user['password']) | |
dl.login() | |
groups = dl.groups() | |
for group in groups: | |
members = dl.members(group.gid) | |
for member in members: | |
member = dl.member(member.mid) | |
print member.name, member.torina, member.rating |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment