﻿# coding=utf-8
import requests
from urllib.parse import urlparse


class Login(object):

    """docstring for Login"""

    def __init__(self, **signin):
        self.url = signin['url']
        if not signin['verify']:
            self.logindata = signin['logindata']
            self.loginurl = signin['loginurl']
            self.login()
        else:
            self.cookies = signin['cookies']

    def login(self):
        response = requests.post(self.loginurl, data=self.logindata)
        self.cookies = response.cookies
        return response

    def cookie(self):
        pass

    def setCookies(self, dict):
        self.cookies = dict

    def getheader(self):
        pass

    def register(self):
        self.setheader()
        url = "http://bbs.ithome.com/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&sign_as=1&inajax=1"
        data = {"formhash": "14acb1bd",
                "qdxq": "nu",
                "qdmode": "2",
                "todaysay": "",
                "fastreply": "0"}
        self.cookies.update({"discuz_2132_sendmail":"1","discuz_2132_nofavfid":"1","discuz_2132_connect_is_bind":"0"})
        return requests.post(url, data=data, cookies=self.cookies, headers=self.headers).text

    def setheader(self):
        url = urlparse(self.url)
        Referer = url.scheme + "://" + url.netloc
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0",
            "Referer": Referer,
            "Host": url.netloc,
            "Connection": "keep-alive",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
        }

    def get(self):
        self.setheader()
        return requests.get(self.url, cookies=self.cookies, headers=self.headers).text

    def getWithCookies(self):
        self.setheader()
        return requests.get(self.url, cookies=self.cookies, headers=self.headers)

    def postWithCookies(self):
        pass

login = Login(
    verify=True,
    url="http://bbs.3dmgame.com/home.php?mod=task&do=apply&id=9",
    loginurl="http://bbs.ithome.com/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&lssubmit=yes",
    logindata={"username": "", "password": ""},
    # url="http://bbs.ithome.com/home.php?mod=spacecp&ac=usergroup",
    cookies=dict(
        uchome_2132_auth="5b3bx5iQyK2yuyALeoJChdkEnVAbAJZnz%2FPV5yjjdfpV9nW7JWrzPG21s1Sr8zq1eTzxPYTqCNFRm36S3%2FoGduom8AHI",
        uchome_2132_cookiereport="a163d4ArUbOfwx5PFQdjebMJMkqzbiRCt5N2M3%2F7yH5gpTMK%2BfCt",
        uchome_2132_checkpm="0",
        uchome_2132_connect_is_bind="0",
        uchome_2132_home_diymode="1",
        uchome_2132_lastact="1409282454%09home.php%09task",
        uchome_2132_lastvisit="1409278415",
        uchome_2132_noticeTitle="1",
        uchome_2132_saltkey="NGlCLgkr",
        uchome_2132_sid="C99oC1"
    ))
print(login.getWithCookies().text)
