add_links_to_db 3.32 KB
Newer Older
Hiro's avatar
Hiro committed
1
2
3
4
5
6
7
8
9
10
11
12
13
#!/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
import sqlite3
Hiro's avatar
Hiro committed
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
import argparse
from urllib import request

def print_header():
    header = """
                             __     __
                            /\ \__ /\ \__
                  __      __\ \ ,_\\\ \ ,_\   ____   _  __
                /'_ `\  /'__`\ \ \/ \ \ \/  / __ `\/\`'__\
               /\ \L\ \/\  __/\ \ \_ \ \ \ /\ \L\  \ \ \/
               \ \____ \ \____\\\__\ \ \ \__\ \_____/\ \_\
                \/___L\ \/____/ \/__/  \/__/\/___/  \/_/
                 /\_____/
                 \_/___/

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

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

def main():
Philipp Winter's avatar
Philipp Winter committed
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
    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)

    webFile = request.urlopen("https://www.torproject.org/projects/torbrowser/RecommendedTBBVersions/")
    versions = webFile.read().decode('utf-8').translate({ord(c): None for c in '[]\n" '})
    version = versions.split(",")[-12]

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


    prefixes = {
        "osx": "TorBrowser-",
        "windows": "torbrowser-install-",
        "linux": "tor-browser-linux64-"
    }

    versions = {"windows": version, 'linux': version, 'osx': version}

    suffixes = {
        "osx": "-osx64_en-US.dmg",
        "windows": "_en-US.exe",
        "linux": "_en-US.tar.xz"
    }

    keys = ['osx', 'windows', 'linux']

    languages = ['en-US', 'es-ES', 'pt-BR']

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

    if not abs_filename:
        print("Missing database filename.")
    else:
        conn = sqlite3.connect(abs_filename)
        with conn:
            c = conn.cursor()
            """
            Here we drop previous links TABLE but probably it would be better to just update old links to INACTIVE
            """
            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)"
                          "VALUES ('%s', '%s', '%s', '64', '%s', '%s', 'ACTIVE')" %(providers.get(p) + release_link, k, l, version, p))
Hiro's avatar
Hiro committed
107
108
109
110
111


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