aboutsummaryrefslogtreecommitdiff
path: root/pydora/configure.py
blob: d58d445b07181e88e9829845c7e36812f755f2ce (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import re
import sys
import requests
from configparser import ConfigParser

from pandora.client import APIClient
from pandora.clientbuilder import PydoraConfigFileBuilder

from .utils import Screen, Colors


class Umask:
    """Set/Restore Umask Context Manager
    """

    def __init__(self, umask):
        self.umask = umask
        self.old_umask = None

    def __enter__(self):
        self.old_umask = os.umask(self.umask)

    def __exit__(self, exc_type, exc_val, exc_tb):
        os.umask(self.old_umask)


class PandoraKeysConfigParser:
    """Parser for Pandora Keys Source Page

    This is an extremely naive restructured text parser designed only to parse
    the pandora API docs keys source file.
    """

    KEYS_URL = (
        "http://6xq.net/git/lars/pandora-apidoc.git/plain/json/partners.rst"
    )

    FIELD_RE = re.compile(
        ":(?P<key>[^:]+): (?:`{2})?(?P<value>[^`\n]+)(?:`{2})?$"
    )

    def _fixup_key(self, key):
        key = key.lower()

        if key.startswith("dec") and "password" in key:
            return "decryption_key"
        elif key.startswith("encrypt"):
            return "encryption_key"
        elif key == "deviceid":
            return "device"
        else:
            return key

    def _format_api_host(self, host):
        return "{}/services/json/".format(host)

    def _clean_device_name(self, name):
        return re.sub("[^a-z]+", "_", name, flags=re.I)

    def _fetch_config(self):
        return requests.get(self.KEYS_URL).text.split("\n")

    def _match_key(self, line):
        key_match = self.FIELD_RE.match(line)
        if key_match:
            match = key_match.groupdict()
            match["key"] = self._fixup_key(match["key"])
            return match
        else:
            return None

    def _is_host_terminator(self, line):
        return line.startswith("--")

    def _is_device_terminator(self, line):
        return line.startswith("^^")

    def load(self):
        buffer = []
        current_partner = {}
        api_host = None
        partners = {}

        for line in self._fetch_config():
            key_match = self._match_key(line)
            if key_match:
                current_partner[key_match["key"]] = key_match["value"]
            elif self._is_host_terminator(line):
                api_host = buffer.pop()
            elif self._is_device_terminator(line):
                key = self._clean_device_name(buffer.pop())
                current_partner = partners[key] = {
                    "api_host": self._format_api_host(api_host)
                }

            buffer.append(line.strip().lower())

        return partners


class Configurator:
    """Interactive Configuration Builder

    Allows a user to configure pydora interactively. Ultimately writes the
    pydora config file.
    """

    def __init__(self):
        self.builder = PydoraConfigFileBuilder()

        self.cfg = ConfigParser()
        self.screen = Screen()

        if self.builder.file_exists:
            self.read_config()
        else:
            self.cfg.add_section("user")
            self.cfg.add_section("api")

    def fail(self, message):
        print(self.screen.print_error(message))
        sys.exit(1)

    def finished(self, message):
        self.screen.print_success(message)
        sys.exit(0)

    def print_message(self, message):
        print(Colors.cyan(message))

    def get_partner_config(self):
        try:
            return PandoraKeysConfigParser().load()["android"]
        except Exception:
            self.fail("Error loading config file. Unable to continue.")

    def get_value(self, section, key, prompt):
        self.cfg.set(section, key, self.screen.get_string(prompt))

    def get_password(self, section, key, prompt):
        self.cfg.set(section, key, self.screen.get_password(prompt))

    def set_static_value(self, section, key, value):
        self.cfg.set(section, key, value)

    def add_partner_config(self, config):
        for key, value in config.items():
            self.cfg.set("api", key, value)

    def read_config(self):
        with open(self.builder.path) as file:
            self.cfg.read_file(file)

    def write_config(self):
        with Umask(0o077), open(self.builder.path, "w") as file:
            self.cfg.write(file)

    def configure(self):
        if self.builder.file_exists:
            self.print_message("You already have a pydora config.")
            self.add_partner_config(self.get_partner_config())
            self.write_config()
            self.finished("Freshened your API keys!")

        self.print_message("Welcome to Pydora, let's configure a few things")
        self.add_partner_config(self.get_partner_config())
        self.get_value("user", "username", "Pandora Username: ")
        self.get_password("user", "password", "Pandora Password: ")
        self.set_static_value(
            "api", "default_audio_quality", APIClient.HIGH_AUDIO_QUALITY
        )

        self.write_config()


def main():
    Configurator().configure()