<commit_before>#!/usr/bin/python
# -*- encoding: utf8 -*-

# © 2014 <NAME>
# License: MIT (see LICENSE)

import mechanize
import cookielib
import xdg.BaseDirectory
import os.path
import ConfigParser
import getpass
import json
import webbrowser

# setup
cookiefile = os.path.join(xdg.BaseDirectory.save_data_path("yucata-tools"), "cookies")
cookiejar =cookielib.LWPCookieJar(cookiefile)
try:
    cookiejar.load()
except IOError:
    pass

br = mechanize.Browser()
br.addheaders = [('User-agent', 'yucata-tools (https://github.com/nomeata/yucata-tools)')]
br.set_cookiejar(cookiejar)


configfile = os.path.join(xdg.BaseDirectory.save_config_path("yucata-tools"), "config.ini")
config = ConfigParser.RawConfigParser()
config.read(configfile)

if not config.has_section('Login'):
    config.add_section('Login')

login = None
if config.has_option('Login', 'username'):
    login = config.get('Login', 'username')


def get_cookie():
    global login
    prompt = "Login [%s]: " % login if login else "Login: "
    thelogin = raw_input(prompt)
    if thelogin:
        login = thelogin
    password = <PASSWORD>()

    config.set('Login', 'username', login)
    config.write(file(configfile, 'wb'))

    br.open("http://www.yucata.de/de")
    br.select_form('aspnetForm')
    br.form["ctl00$ctl07$edtLogin"] = login
    br.form["ctl00$ctl07$edtPassword"] = password
    br.form["ctl00$ctl07$cbxCookie"] = ('on',)
    br.submit(name="ctl00$ctl07$btnLogin", label="Anmelden")
    cookiejar.save()

def try_get_games():
    try:
        resp = br.open("http://www.yucata.de/Services/YucataService.svc/GetCurrentGames", data="")
        return resp.read()
    except mechanize.HTTPError, e:
        if int(e.code) == 500:
            return None
        else:
            raise
        
def get_games():
    games = try_get_games()
    if games is None:
        print "Cannot fetch games. Trying to log in first."
        get_cookie()
        games = try_get_games()
    if games is None:
        print "Sorry, cannot fetch games"
        return

    data = json.loads(games)
    #print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

    games = data["d"]["Games"]
    games_on_turn = []
    next_game = None

    next_game_id = data["d"]["NextGameOnTurn"]
<commit_msg>Switch to using urllib instead of mechanize<commit_after>#!/usr/bin/python
# -*- encoding: utf8 -*-

# © 2014 <NAME>
# License: MIT (see LICENSE)

import urllib
import urllib2
import cookielib
import xdg.BaseDirectory
import os.path
import ConfigParser
import getpass
import json
import webbrowser
import re

# setup
cookiefile = os.path.join(xdg.BaseDirectory.save_data_path("yucata-tools"), "cookies")
cookiejar =cookielib.LWPCookieJar(cookiefile)
try:
    cookiejar.load()
except IOError:
    pass

br = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
br.addheaders = [('User-agent', 'yucata-tools (https://github.com/nomeata/yucata-tools)')]


configfile = os.path.join(xdg.BaseDirectory.save_config_path("yucata-tools"), "config.ini")
config = ConfigParser.RawConfigParser()
config.read(configfile)

if not config.has_section('Login'):
    config.add_section('Login')

login = None
if config.has_option('Login', 'username'):
    login = config.get('Login', 'username')


def get_cookie():
    global login
    prompt = "Login [%s]: " % login if login else "Login: "
    thelogin = raw_input(prompt)
    if thelogin:
        login = thelogin
    password = <PASSWORD>()

    config.set('Login', 'username', login)
    config.write(file(configfile, 'wb'))

    page = br.open("http://www.yucata.de/de").read()

    viewstate_re = re.compile(r'id="__VIEWSTATE" value="([^"]+)"')
    m = viewstate_re.search(page)
    if not m:
        raise Exception("No __VIEWSTATE found")
    viewstate = m.group(1)

    viewstate_gen_re = re.compile(r'id="__VIEWSTATEGENERATOR" value="([^"]+)"')
    m = viewstate_gen_re.search(page)
    if not m:
        raise Exception("No __VIEWSTATEGENERATOR found")
    viewstate_gen = m.group(1)
    
    query = urllib.urlencode({
            "ctl00$ctl07$edtLogin": login,
            "ctl00$ctl07$edtPassword": password,
            "ctl00$ctl07$cbxCookie": 'on',
            "__VIEWSTATE": viewstate,
            "__VIEWSTATEGENERATOR": viewstate_gen,
            "ctl00$ctl07$btnLogin": "Anmelden",
            })
    page = br.open("http://www.yucata.de/de",query).read()
    cookiejar.save()

def try_get_games():
    try:
        resp = br.open("http://www.yucata.de/Services/YucataService.svc/GetCurrentGames", data="")
        return resp.read()
    except urllib2.HTTPError, e:
        if int(e.code) == 500:
            return None
        else:
            raise
        
def get_games():
    games = try_get_games()
    if games is None:
        print "Cannot fetch games. Trying to log in first."
        get_cookie()
        games = try_get_games()
    if games is None:
        print "Sorry, cannot fetch games"
        return

    data = json.loads(games)
    #print json.dumps(data, sort_keys=True, indent=4, separators=(',', ': '))

    games = data["d"]["Games"]
    games_on_turn = []
    next_game = None

    next_game_id = data["d"]["NextGameOnTurn"]

{'fraction_non_alphanumeric': 0.14423076923076922, 'fraction_numerical': 0.010769230769230769, 'mean_word_length': 3.3054635761589406, 'pattern_counts': {'":': 6, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2513468', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1762, 'n_words': 369}