#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-

import urllib  
import urllib2  
import cookielib
import getpass
import re
import json
import string
import os

filename = 'cookie'
if os.path.exists(filename): 
  cookie = cookielib.MozillaCookieJar()
  cookie.load('cookie.txt', ignore_discard=True, ignore_expires=True)
else: 
  cookie = cookielib.MozillaCookieJar(filename)
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie))

def login():
  print 'please login'
  username = raw_input('username:');
  password = getpass.getpass('password:');

  postdata = urllib.urlencode({
             'nextpage': 'http://community.topcoder.com/stat?c=problem_solution&cr=10574855&rd=16554&pm=13986',
             'module': 'Login',
             'username': username,
             'password': password
             })
  loginUrl = 'https://community.topcoder.com/tc';
  result = opener.open(loginUrl,postdata)
  cookie.save(ignore_discard=True, ignore_expires=True)

def getpage(url):
  result = opener.open(url)
  return result.read()

def getDataUrl(problemUrl):
  html = getpage(problemUrl);
  ref = re.findall(r'(/tc\?module=ProblemDetail[^"]*)"', html);
  assert len(ref) > 0
  statusUrl = 'http://community.topcoder.com' + ref[0];

  html = getpage(statusUrl);
  ref = re.findall(r'<a href="([^"]*)" class="statText">view</a>', html);
  assert len(ref) > 0
  return 'http://community.topcoder.com' + ref[0].replace('&amp;', '&');

def pretty(s):
  s = '[' + s.replace('\n', '').replace('{', '[').replace('}', ']') + ']';
  return json.loads(s);

def output(data, layout):
  def getLabel(i, j):
    return string.uppercase[i] + '.' * j
  
  def checkTypeStr(s):
    return isinstance(s, str) or isinstance(s, unicode);

  for i, s in enumerate(data):
    if isinstance(s, list) and checkTypeStr(s[0]):
      layout = layout.replace(getLabel(i, 2), str(len(s[0])));
  for i, s in enumerate(data):
    if isinstance(s, list):
      layout = layout.replace(getLabel(i, 1), str(len(s)));
  for i, s in enumerate(data):
    if isinstance(s, list):
      layout = layout.replace(getLabel(i, 0), 
          ('\n' if checkTypeStr(s[0]) else ' ').join(map(str, s)));
    else:
      layout = layout.replace(getLabel(i, 0), str(s));
  return layout

def generateData(url):
  while True:
    login();
    html = getpage(url);
    text = re.findall(r'<!-- System Testing -->([^!]*)!', html);
    if len(text) > 0: break;
    print 'wrong password?'
  data = re.findall(r'<TD (?:BACKGROUND="/i/steel_blue_bg.gif" )?CLASS="statText"[^>]*>([^<]*)</TD>', text[0])
  assert len(data) % 3 == 0
  dataIn = map(pretty, data[::3]);
  dataOut = map(pretty, data[1::3]);
  
  count = len(dataIn);
  name = raw_input('file name:');
  group = input('group size:');
  
  handle = open('layout.in');
  layoutIn = handle.read();
  handle.close();

  handle = open('layout.out');
  layoutOut = handle.read();
  handle.close();

  if os.path.exists(name) == False: os.mkdir(name);
  for i in xrange(0, count, group):
    writerIn = open('%s/%s%03d.in' % (name, name, i / group + 1), 'w');
    writerOut = open('%s/%s%03d.out' % (name, name, i / group + 1), 'w');
    
    if group > 1: writerIn.write(str(min(count - i, group)) + '\n');

    for j in xrange(i, min(count, i + group)) :
      writerIn.write(output(dataIn[j], layoutIn));
      writerOut.write(output(dataOut[j], layoutOut));
    writerIn.close();
    writerOut.close();

problemUrl = raw_input('problem url:');
dataUrl = getDataUrl(problemUrl);
print 'data url: ', dataUrl
generateData(dataUrl);


@@ -243,16 +243,24 @@
 okieJar(
+filename
 )%0A  cook
@@ -271,20 +271,16 @@
 oad(
-'cookie.txt'
+filename
 , ig
@@ -872,36 +872,23 @@
 /tc';%0A  
-result = opener.open
+getpage
 (loginUr
@@ -889,16 +889,17 @@
 oginUrl,
+ 
 postdata
@@ -972,18 +972,63 @@
 page(url
-):
+, postData = None):%0A  print 'send request', url
 %0A  resul
@@ -1050,18 +1050,27 @@
 (url
+, postData
 )%0A  ret
-urn
+ =
  res
@@ -1079,16 +1079,30 @@
 t.read()
+%0A  return ret;
 %0A%0Adef ge
@@ -1725,16 +1725,64 @@
 Str(s):%0A
+    if len(s) == 0: return False;%0A    s = s%5B0%5D;%0A
     retu
@@ -1907,19 +1907,16 @@
 ypeStr(s
-%5B0%5D
 ):%0A     
@@ -2231,19 +2231,16 @@
 ypeStr(s
-%5B0%5D
 ) else '
@@ -2368,16 +2368,34 @@
 a(url):%0A
+  firstIn = True;%0A
   while 
@@ -2404,21 +2404,8 @@
 ue:%0A
-    login();%0A
     
@@ -2527,31 +2527,101 @@
     
-print 'wrong password?'
+if firstIn: firstIn = False; %0A    else: print 'user name or password is wrong?';%0A    login();
 %0A  d
@@ -3627,16 +3627,16 @@
 url:');%0A
+
 dataUrl 
@@ -3665,36 +3665,8 @@
 l);%0A
-print 'data url: ', dataUrl%0A
 gene

fix bug