about summary refs log tree commit diff stats
path: root/cpp/072scenario_screen.cc
diff options
context:
space:
mode:
Diffstat (limited to 'cpp/072scenario_screen.cc')
0 files changed, 0 insertions, 0 deletions
> 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
a>
88
89
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
                        






































                                                                                                          
                                                                                                             










                                                                             
                                                               

                             




                                                                




















                                                                                                                             


                                            






























                                                                                                                          
                          
             
#!/usr/local/bin/python3
import os, csv, json
from datetime import datetime
from flask import Flask, request, render_template

INPUT_PATH = os.path.join("tntvillage-release-dump", "tntvillage-release-dump.csv")
CSV_SEPARATOR = ","
HEADER = [ "DATA", "HASH", "TOPIC", "POST", "AUTORE", "TITOLO", "DESCRIZIONE", "DIMENSIONE", "CATEGORIA" ]
TABLE_HEADER = [ "DATA", "CATEGORIA", "TITOLO", "DESCRIZIONE", "AUTORE", "DIMENSIONE", "HASH" ]
CATEGORIE = {
    1: "Film TV e programmi",
    2: "Musica",
    3: "E Books",
    4: "Film",
    6: "Linux",
    7: "Anime",
    8: "Cartoni",
    9: "Macintosh",
    10: "Windows Software",
    11: "Pc Game",
    12: "Playstation",
    13: "Students Releases",
    14: "Documentari",
    21: "Video Musicali",
    22: "Sport",
    23: "Teatro",
    24: "Wrestling",
    25: "Varie",
    26: "Xbox",
    27: "Immagini sfondi",
    28: "Altri Giochi",
    29: "Serie TV",
    30: "Fumetteria",
    31: "Trash",
    32: "Nintendo",
    34: "A Book",
    35: "Podcast",
    36: "Edicola",
    37: "Mobile"
}
MAGNET_STR = '<button onclick=\"location.href=\'magnet:?xt=urn:btih:{}\'\" class="btn btn-danger">n</button>'

def handle_content(content: str):
    return { x: content[idx] for idx, x in enumerate(HEADER) }
    
def search_keyword(content, keyword: str, category=0):
    results = [ x for x in content if keyword.lower() in x['TITOLO'].lower()]
    if category == 0:
        return results
    return [ x for x in results if category == x['CATEGORIA']]

def get_last_torrents(content, page=1, amt=50):
    tmp = sorted(content, key=lambda x:x['DATA'], reverse=True)
    tmp_length = len(content)
    
    offset = amt * page
    start_from = offset - amt
    start_from = 0 if start_from < 0 else start_from
    end_with = start_from + amt
    end_with = tmp_length if end_with > tmp_length else end_with
    return tmp[start_from:end_with]

def load_content(input_path=INPUT_PATH):
    with open(input_path, "r", encoding="utf-8") as in_file:
        csv_iterator = csv.reader(in_file, quotechar='"', delimiter=',', quoting=csv.QUOTE_NONNUMERIC, skipinitialspace=True)
        next(csv_iterator)
        return [ handle_content(x) for x in csv_iterator ]

def sizeof_fmt(num, suffix="B"):
    for unit in ("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"):
        if abs(num) < 1024.0:
            return f"{num:3.1f}{unit}{suffix}"
        num /= 1024.0
    return f"{num:.1f}Yi{suffix}"

def parse_values(result):
    tmp = result.copy()
    tmp['DATA'] = datetime.strptime(tmp['DATA'], "%Y-%m-%dT%H:%M:%S").strftime("%d/%m/%Y")
    tmp['CATEGORIA'] = CATEGORIE[int(tmp['CATEGORIA'])]
    tmp['HASH'] = MAGNET_STR.format(tmp['HASH'])
    tmp['DIMENSIONE'] = sizeof_fmt(tmp['DIMENSIONE'])
    return tmp

def format_results(results, headers=HEADER):
    contents = [parse_values(x) for x in results]
    return [[result[header] for header in headers] for result in contents]

def get_args(args):
    keywords = args.get("keywords") or ""
    category = args.get("category") or 0
    page = args.get("page") or 1
    return keywords, category, page

content = load_content()
app = Flask(__name__)

@app.route('/api/header')
def route_api_header():
    return json.dumps(HEADER)

@app.route('/api')
def route_api():
    keywords, category, page = get_args(request.args)
    results = search_keyword(content, keywords, int(category))
    results = get_last_torrents(results, page=int(page))
    return json.dumps(results)

@app.route('/')
def route_main():
    keywords, category, page = get_args(request.args)
    results = search_keyword(content, keywords, int(category))
    results = get_last_torrents(results, page=int(page))
    results = format_results(results, headers=TABLE_HEADER)
    return render_template("index.html", headers=TABLE_HEADER, content=results, categories=CATEGORIE.items(), page=page)  

if __name__ == '__main__':
    app.run()