add_links_to_db 3.82 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
        "linux": "tor-browser-linux64-"
Philipp Winter's avatar
Philipp Winter committed
79
80
    }

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

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

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

    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
124
125
126

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

Philipp Winter's avatar
Philipp Winter committed
127
128
129
130
    conn = sqlite3.connect(abs_filename)
    with conn:
        c = conn.cursor()
        """
131
132
        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
133
134
135
        """
        c.execute("DROP TABLE IF EXISTS links")
        c.execute(
Hiro's avatar
Hiro committed
136
137
            "CREATE TABLE links(link TEXT, platform TEXT, language TEXT,"
            " arch TEXT, version TEXT, provider TEXT, status TEXT, file TEXT)"
Philipp Winter's avatar
Philipp Winter committed
138
139
140
141
142
143
        )
        for k in keys:
            for p in providers:
                for l in languages:
                    release_link = releases.get(k).replace("en-US", l)
                    c.execute(
Hiro's avatar
Hiro committed
144
                        "INSERT INTO links(link, platform, language, arch, version, provider, status, file)"
145
                        "VALUES ('%s', '%s', '%s', '64', '%s', '%s', 'ACTIVE', '%s')" % (providers.get(p) + release_link, k, l, version, p, release_link))
Hiro's avatar
Hiro committed
146
147
148
149
150


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