summaryrefslogtreecommitdiff
path: root/plugin.video.watchbox/resources/lib/login.py
blob: 58a1b5eb4bf430cbe497eedf962311047adac93d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
# Watchbox
# Copyright (C) 2017 MrKrabat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import cookielib
import urllib
import urllib2

import xbmc


def login(username, password, args):
    """Login and session handler
    """
    login_url = "https://www.watchbox.de/login"

    # create cookie path
    cookiepath = os.path.join(
        xbmc.translatePath(args._addon.getAddonInfo("profile")).decode("utf-8"),
        "cookies.lwp")

    # create cookiejar
    cj = cookielib.LWPCookieJar()
    args._cj = cj

    # lets urllib2 handle cookies
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36")]
    opener.addheaders = [("Accept-Charset", "utf-8")]
    urllib2.install_opener(opener)

    # check if session exists
    try:
        cj.load(cookiepath, ignore_discard=True)

        # check if session is valid
        response = urllib2.urlopen("https://www.watchbox.de/")
        html = response.read()

        if username in html:
            # session is valid
            return True

    except IOError:
        # cookie file does not exist
        pass

    # build POST data
    post_data = urllib.urlencode({"email":    username,
                                  "password": password})

    # POST to login page
    response = urllib2.urlopen(login_url, post_data)

    # check for login string
    html = response.read()

    if username in html:
        # save session to disk
        cj.save(cookiepath, ignore_discard=True)
        return True
    else:
        return False


def getCookie(args):
    """Returns all cookies as string and urlencoded
    """
    if not args._login:
        return "|User-Agent=Mozilla%2F5.0%20%28Windows%20NT%2010.0%3B%20Win64%3B%20x64%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F60.0.3112.113%20Safari%2F537.36"

    # create cookie path
    cookiepath = os.path.join(
        xbmc.translatePath(args._addon.getAddonInfo("profile")).decode("utf-8"),
        "cookies.lwp")
    # save session to disk
    args._cj.save(cookiepath, ignore_discard=True)

    ret = ""
    for cookie in args._cj:
        ret += urllib.urlencode({cookie.name : cookie.value}) + ";"

    return "|User-Agent=Mozilla%2F5.0%20%28Windows%20NT%2010.0%3B%20Win64%3B%20x64%29%20AppleWebKit%2F537.36%20%28KHTML%2C%20like%20Gecko%29%20Chrome%2F60.0.3112.113%20Safari%2F537.36&Cookie=" + ret[:-1]