#!/usr/bin/python

import re
from sys import exit

import requests

from config import *
if LOGIN_NAME == '' or LOGIN_PASS == '':
    print('Please set LOGIN_NAME and LOGIN_PASS in config.py.')
    exit(1)


class V2ex(object):

    def __init__(self):
        self.session = requests.Session()
        self.session.headers.update(BROWSER_HEADER)

        self.auth_dict = {}
        self.redeem_code = ''

        if NOTIFIER_WEBHOOK_URL != '':
            self.notifier = Notifier()


    def __prepare_login_data(self):
        r = self.session.get(LOGIN_URL)

        user_name = re.search(USER_PATTERN, r.text).groups()[0]
        pass_name = re.search(PASS_PATTERN, r.text).groups()[0]
        once_value = re.search(ONCE_PATTERN, r.text).groups()[0]

        self.auth_dict = {
            user_name: LOGIN_NAME,
            pass_name: LOGIN_PASS,
            'once': once_value
        }


    def __prepare_redeem_param(self):
        r = self.session.get(DAILY_URL)
        try:
            self.redeem_code = re.search(REDEEM_PATTERN, r.text).groups()[0]
        except:
            if self.notifier:
                self.notifier.notify('already redeemed for today.')
            exit(1)


    def login(self):
        self.__prepare_login_data()
        r = self.session.post(LOGIN_URL, self.auth_dict)
        return self


    def redeem(self):
        self.__prepare_redeem_param()
        r = self.session.get(REDEEM_URL + self.redeem_code)
        if self.notifier:
            self.notifier.notify('redeem works.')


class Notifier(object):

    def notify(self, message):
        requests.post(NOTIFIER_WEBHOOK_URL, json={"text":message, "username": "v2ex_redeem"})


if __name__ == '__main__':
    v2ex = V2ex()
    v2ex.login().redeem()

@@ -400,16 +400,55 @@
 e = ''%0A%0A
+        self.notifier = NullNotifier()%0A
         
@@ -1130,92 +1130,115 @@
     
-if self.notifier:%0A                self.notifier.notify('already redeemed for today.'
+message = 'already redeemed for today.'%0A            print(message)%0A            self.notifier.notify(message
 )%0A  
@@ -1525,75 +1525,281 @@
     
-if self.notifier:%0A            self.n
+message = 'redeem works.'%0A        print(message)%0A        self.notifier.notify(message)%0A%0A%0Aclass NotifierInterface(object):%0A%0A    def notify(self, message):%0A        raise NotImplementedError%0A%0A%0Aclass NullN
 otifier
-.n
+(N
 otif
-y('redeem works.')
+ierInterface):%0A%0A    def notify(self, message):%0A        pass
 %0A%0A%0Ac
@@ -1812,22 +1812,33 @@
 otifier(
-object
+NotifierInterface
 ):%0A%0A    

Use a NullNotifier when no webhook url available