#!/usr/bin/env python
#-*- coding: utf-8 -*-

from string import maketrans

import requests
from lxml import html
from v2ex_config import USER, PASSWD


def get_once(page_text):
    tree = html.fromstring(page_text)
    once = tree.xpath('//input[@name="once"]/@value')[0]
    return once


def login(url):
    r = s.get(url, verify=False)
    login_data = {
        'u': USER,
        'p': PASSWD,
        'once': get_once(r.text),
        'next': '/'
    }
    headers = {'Referer': 'https://www.v2ex.com/signin'}
    s.post(url, headers=headers, data=login_data)


def get_money(mission_url):
    r = s.get(mission_url, verify=False)
    tree = html.fromstring(r.text)

    raw_once = tree.xpath('//input[@type="button"]/@onclick')[0]
    table = maketrans('', '')
    once = raw_once.split('=', 1)[1].translate(table, " ';")
    if once == '/balance':
        print "You have completed the mission today."
        return
    else:
        s.get('https://www.v2ex.com'+once, verify=False)
        get_balance(balance_url)


def get_balance(balance_url):
    r = s.get(balance_url, verify=False)
    tree = html.fromstring(r.text)
    total = tree.xpath('//table[@class="data"]/tr[2]/td[4]/text()')[0]
    today = tree.xpath('//table[@class="data"]/tr[2]/td[5]/span/text()')[0]
    print "Today: " + today
    print "Total: " + total


s = requests.session()
s.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; \
                   rv:28.0) Gecko/20100101 Firefox/28.0'})

signin_url = 'https://www.v2ex.com/signin'
balance_url = 'https://www.v2ex.com/balance'
mission_url = 'https://www.v2ex.com/mission/daily'

login(signin_url)
get_money(mission_url)








@@ -16,16 +16,17 @@
 python%0A#
+ 
 -*- codi
@@ -68,16 +68,62 @@
 aketrans
+%0Aimport logging%0Aimport sys%0Afrom os import path
 %0A%0Aimport
@@ -1316,24 +1316,61 @@
 text()')%5B0%5D%0A
+    logging.info(today+'    '+total)%0A
     print %22T
@@ -1415,16 +1415,159 @@
 total%0A%0A%0A
+logging.basicConfig(%0A    filename=path.join(sys.path%5B0%5D, 'v2ex.log'),%0A    level='INFO',%0A    formate='%25(asctime)s %5B%25(levelname)s%5D %25(message)s')%0A
 s = requ
@@ -1900,12 +1900,4 @@
 url)
-%0A%0A%0A%0A%0A%0A%0A%0A

add log