add_lins_to_db 3 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
56
  args = parser.parse_args()
  abs_filename = os.path.abspath(args.filename)

  webFile = request.urlopen("https://lektor-staging.torproject.org/tpo/staging/projects/torbrowser/RecommendedTBBVersions/")
Hiro's avatar
Hiro committed
57
  versions = webFile.read().decode('utf-8')
Hiro's avatar
Hiro committed
58
59
  version = versions.split("&#34;")[1]

Hiro's avatar
Hiro committed
60
61
62
63
64
65
  providers = {
    "osx": "https://gitlab.com/hiromipaw/torbrowser/raw/releases/",
    "windows": "https://gitlab.com/hiromipaw/torbrowser/raw/releases/",
    "linux": "https://gitlab.com/hiromipaw/torbrowser/raw/releases/"
  }

Hiro's avatar
Hiro committed
66
67

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

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

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

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


  if not abs_filename:
      print("Missing database filename.")
Hiro's avatar
Hiro committed
87
  else:
Hiro's avatar
Hiro committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
      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, arch TEXT,"
              " version TEXT, provider TEXT, status TEXT)"
          )
          for k in keys:
              c.execute(
                  "INSERT INTO links(link, platform, arch, version, provider, status)"
Hiro's avatar
Hiro committed
102
                  "VALUES ('%s', '%s', '64', '%s', 'gitlab', 'ACTIVE')" %(releases.get(k), k, version)
Hiro's avatar
Hiro committed
103
              )
Hiro's avatar
Hiro committed
104
105
106
107
108
109


if __name__ == "__main__":
    print_header()
    main()
    print_footer