add_links_to_db 3.19 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():
Hiro's avatar
Hiro committed
44
45
46
47
48
49
50
51
52
  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."
  )

Hiro's avatar
Hiro committed
53
54
55
  args = parser.parse_args()
  abs_filename = os.path.abspath(args.filename)

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

Hiro's avatar
Hiro committed
60
  providers = {
Hiro's avatar
Hiro committed
61
62
    "gitlab": "https://gitlab.com/thetorproject/gettorbrowser/raw/releases/",
    "github": "https://github.com/TheTorProject/gettorbrowser/raw/torbrowser-releases/"
Hiro's avatar
Hiro committed
63
64
  }

Hiro's avatar
Hiro committed
65
66

  prefixes = {
Hiro's avatar
Hiro committed
67
68
69
    "osx": "TorBrowser-",
    "windows": "torbrowser-install-",
    "linux": "tor-browser-linux64-"
Hiro's avatar
Hiro committed
70
71
72
73
74
75
76
77
78
79
  }

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

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

Hiro's avatar
Hiro committed
80
81
82
  keys = ['osx', 'windows', 'linux']

  languages = ['en-US', 'es-ES', 'pt-BR']
Hiro's avatar
Hiro committed
83

Hiro's avatar
Hiro committed
84
  releases = {k: "".join(dic.get(k, version) for dic in (prefixes, versions, suffixes))  for k in keys}
Hiro's avatar
Hiro committed
85
86
87

  if not abs_filename:
      print("Missing database filename.")
Hiro's avatar
Hiro committed
88
  else:
Hiro's avatar
Hiro committed
89
90
91
92
93
94
95
96
      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(
Hiro's avatar
Hiro committed
97
              "CREATE TABLE links(link TEXT, platform TEXT, language TEXT, arch TEXT,"
Hiro's avatar
Hiro committed
98
99
100
              " version TEXT, provider TEXT, status TEXT)"
          )
          for k in keys:
Hiro's avatar
Hiro committed
101
              for p in providers:
Hiro's avatar
Hiro committed
102
103
104
                  for l in languages:
                      release_link = releases.get(k).replace("en-US", l)
                      c.execute(
Hiro's avatar
Hiro committed
105
                        "INSERT INTO links(link, platform, language, arch, version, provider, status)"
Hiro's avatar
Hiro committed
106
                        "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()