add_links_to_db 3.9 KB
Newer Older
Hiro's avatar
Hiro committed
1
2
3
4
5
6
7
8
9
10
11
12
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of GetTor, a Tor Browser distribution system.
#
# :authors: hiro <hiro@torproject.org>
#           see also AUTHORS file
#
# :license: This is Free Software. See LICENSE for license information.

import os
import sys
13
import json
Hiro's avatar
Hiro committed
14
import sqlite3
Hiro's avatar
Hiro committed
15
16
17
import argparse
from urllib import request

18
19
TOR_BROWSER_DOWNLOADS = "https://aus1.torproject.org/torbrowser/update_3/release/downloads.json"

20

Hiro's avatar
Hiro committed
21
def print_header():
22
    header = r"""
Hiro's avatar
Hiro committed
23
24
                             __     __
                            /\ \__ /\ \__
25
                  __      __\ \ ,_\\\ \ ,_\  ____   _  __
Hiro's avatar
Hiro committed
26
27
                /'_ `\  /'__`\ \ \/ \ \ \/  / __ `\/\`'__\
               /\ \L\ \/\  __/\ \ \_ \ \ \ /\ \L\  \ \ \/
28
29
               \ \____ \ \____\\ \__\ \ \__\ \_____/\ \_\
                \/___L\ \/____/ \/__/  \/__/\/____/  \/_/
Hiro's avatar
Hiro committed
30
31
32
33
34
35
36
37
38
39
40
                 /\_____/
                 \_/___/

    """
    print("")
    print("@"*100)
    print("@"*100)
    print(header)
    print("@"*100)
    print("")

41

Hiro's avatar
Hiro committed
42
43
44
45
46
def print_footer():
    print("")
    print("@"*100)
    print("@"*100)
    print("")
Hiro's avatar
Hiro committed
47

48

Hiro's avatar
Hiro committed
49
def main():
Philipp Winter's avatar
Philipp Winter committed
50
51
52
53
54
55
56
57
58
59
60
61
    parser = argparse.ArgumentParser(
        description="Tool to create the gettor SQLite database."
    )

    parser.add_argument(
        "-f", "--filename", default="gettor.db", metavar="gettor.db",
        help="Database filename."
    )

    args = parser.parse_args()
    abs_filename = os.path.abspath(args.filename)

62
63
    webFile = request.urlopen(TOR_BROWSER_DOWNLOADS)
    content = webFile.read()
Hiro's avatar
Hiro committed
64
    jsonObj = json.loads(content.decode('utf-8'))
65
66
67
68
    version = jsonObj.get("version", None)
    if version is None:
        print("Could not fetch version from {}.".format(TOR_BROWSER_DOWNLOADS))
        sys.exit(1)
Philipp Winter's avatar
Philipp Winter committed
69
70
71
72
73
74
75
76
77

    providers = {
        "gitlab": "https://gitlab.com/thetorproject/gettorbrowser/raw/releases/",
        "github": "https://github.com/TheTorProject/gettorbrowser/raw/torbrowser-releases/"
    }

    prefixes = {
        "osx": "TorBrowser-",
        "windows": "torbrowser-install-",
Hiro's avatar
Hiro committed
78
79
        "linux64": "tor-browser-linux64-",
        "linux32": "tor-browser-linux32-"
Philipp Winter's avatar
Philipp Winter committed
80
81
    }

Hiro's avatar
Hiro committed
82
    versions = {"windows": version, 'linux32': version, 'linux64': version, 'osx': version}
Philipp Winter's avatar
Philipp Winter committed
83
84
85
86

    suffixes = {
        "osx": "-osx64_en-US.dmg",
        "windows": "_en-US.exe",
Hiro's avatar
Hiro committed
87
88
        "linux32": "_en-US.tar.xz",
        "linux64": "_en-US.tar.xz"
Philipp Winter's avatar
Philipp Winter committed
89
90
    }

Hiro's avatar
Hiro committed
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
    keys = ['osx', 'windows', 'linux32', 'linux64']

    languages = [
        'en-US',
        'es-ES',
        'pt-BR',
        'ar',
        'ca',
        'cs',
        'da',
        'de',
        'el',
        'es-AR',
        'fa',
        'fr',
        'ga-IE',
        'he',
        'hu',
        'id',
        'is',
        'it',
        'ja',
        'ka',
        'ko',
        'nb-NO',
        'nl',
        'pl',
        'pt-BR',
        'ru',
        'sv-SE',
        'tr',
        'vi',
        'zh-CN',
        'zh-TW'
    ]
Philipp Winter's avatar
Philipp Winter committed
126
127
128

    releases = {k: "".join(dic.get(k, version) for dic in (prefixes, versions, suffixes))  for k in keys}

Philipp Winter's avatar
Philipp Winter committed
129
130
131
132
    conn = sqlite3.connect(abs_filename)
    with conn:
        c = conn.cursor()
        """
133
134
        Here we drop previous links TABLE but probably it would be better to
        just update old links to INACTIVE
Philipp Winter's avatar
Philipp Winter committed
135
136
137
138
139
140
141
142
143
144
145
146
        """
        c.execute("DROP TABLE IF EXISTS links")
        c.execute(
            "CREATE TABLE links(link TEXT, platform TEXT, language TEXT, arch TEXT,"
            " version TEXT, provider TEXT, status TEXT)"
        )
        for k in keys:
            for p in providers:
                for l in languages:
                    release_link = releases.get(k).replace("en-US", l)
                    c.execute(
                        "INSERT INTO links(link, platform, language, arch, version, provider, status)"
147
                        "VALUES ('%s', '%s', '%s', '64', '%s', '%s', 'ACTIVE')" % (providers.get(p) + release_link, k, l, version, p))
Hiro's avatar
Hiro committed
148
149
150
151
152


if __name__ == "__main__":
    print_header()
    main()
Hiro's avatar
Hiro committed
153
    print_footer()