File size: 4,678 Bytes
b5122dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
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
42
43
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
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
import json
import queue
import threading
from typing import List

from models.media import Media
from torrent.torrent_item import TorrentItem
from utils.logger import setup_logger
from utils.string_encoding import encodeb64

logger = setup_logger(__name__)

INSTANTLY_AVAILABLE = "[โšก]"
DOWNLOAD_REQUIRED = "[โฌ‡๏ธ]"
DIRECT_TORRENT = "[๐Ÿดโ€โ˜ ๏ธ]"


# TODO: Languages
def get_emoji(language):
    emoji_dict = {
        "fr": "๐Ÿ‡ซ๐Ÿ‡ท",
        "en": "๐Ÿ‡ฌ๐Ÿ‡ง",
        "es": "๐Ÿ‡ช๐Ÿ‡ธ",
        "de": "๐Ÿ‡ฉ๐Ÿ‡ช",
        "it": "๐Ÿ‡ฎ๐Ÿ‡น",
        "pt": "๐Ÿ‡ต๐Ÿ‡น",
        "ru": "๐Ÿ‡ท๐Ÿ‡บ",
        "in": "๐Ÿ‡ฎ๐Ÿ‡ณ",
        "nl": "๐Ÿ‡ณ๐Ÿ‡ฑ",
        "hu": "๐Ÿ‡ญ๐Ÿ‡บ",
        "la": "๐Ÿ‡ฒ๐Ÿ‡ฝ",
        "multi": "๐ŸŒ"
    }
    return emoji_dict.get(language, "๐Ÿ‡ฌ๐Ÿ‡ง")


def filter_by_availability(item):
    if item["name"].startswith(INSTANTLY_AVAILABLE):
        return 0
    else:
        return 1


def filter_by_direct_torrnet(item):
    if item["name"].startswith(DIRECT_TORRENT):
        return 1
    else:
        return 0


def parse_to_debrid_stream(torrent_item: TorrentItem, configb64, host, torrenting, results: queue.Queue, media: Media):
    if torrent_item.availability == True:
        name = f"{INSTANTLY_AVAILABLE}\n"
    else:
        name = f"{DOWNLOAD_REQUIRED}\n"

    parsed_data = torrent_item.parsed_data.data

    # TODO: Always take the first resolution, is that the best one?
    resolution = parsed_data.resolution[0] if len(parsed_data.resolution) > 0 else "Unknown"
    name += f"{resolution}" + (f"\n({'|'.join(parsed_data.quality)})" if len(
        parsed_data.quality) > 0 else "")

    size_in_gb = round(int(torrent_item.size) / 1024 / 1024 / 1024, 2)

    title = f"{torrent_item.raw_title}\n"

    if torrent_item.file_name is not None:
        title += f"{torrent_item.file_name}\n"

    title += f"๐Ÿ‘ฅ {torrent_item.seeders}   ๐Ÿ’พ {size_in_gb}GB   ๐Ÿ” {torrent_item.indexer}\n"
    if parsed_data.codec:
        title += f"๐ŸŽฅ {parsed_data.codec.upper()}   "
    if parsed_data.audio:
        title += f"๐ŸŽง {', '.join(parsed_data.audio)}"
    if parsed_data.codec or parsed_data.audio:
        title += "\n"

    for language in torrent_item.languages:
        title += f"{get_emoji(language)}/"
    title = title[:-1]

    queryb64 = encodeb64(json.dumps(torrent_item.to_debrid_stream_query(media))).replace('=', '%3D')

    results.put({
        "name": name,
        "description": title,
        "url": f"{host}/playback/{configb64}/{queryb64}",
        "behaviorHints":{
            "bingeGroup": f"stremio-jackett-{torrent_item.info_hash}",
            "filename": torrent_item.file_name if torrent_item.file_name is not None else torrent_item.raw_title # TODO: Use parsed title?
        }
    })

    if torrenting and torrent_item.privacy == "public":
        name = f"{DIRECT_TORRENT}\n{parsed_data.quality}\n"
        if len(parsed_data.quality) > 0 and parsed_data.quality[0] != "Unknown" and \
                parsed_data.quality[0] != "":
            name += f"({'|'.join(parsed_data.quality)})"
        results.put({
            "name": name,
            "description": title,
            "infoHash": torrent_item.info_hash,
            "fileIdx": int(torrent_item.file_index) if torrent_item.file_index else None,
            "behaviorHints":{
                "bingeGroup": f"stremio-jackett-{torrent_item.info_hash}",
                "filename": torrent_item.file_name if torrent_item.file_name is not None else torrent_item.raw_title # TODO: Use parsed title?
            }
            # "sources": ["tracker:" + tracker for tracker in torrent_item.trackers]
        })


def parse_to_stremio_streams(torrent_items: List[TorrentItem], config, media):
    stream_list = []
    threads = []
    thread_results_queue = queue.Queue()

    configb64 = encodeb64(json.dumps(config).replace('=', '%3D'))
    for torrent_item in torrent_items[:int(config['maxResults'])]:
        thread = threading.Thread(target=parse_to_debrid_stream,
                                  args=(torrent_item, configb64, config['addonHost'], config['torrenting'],
                                        thread_results_queue, media),
                                  daemon=True)
        thread.start()
        threads.append(thread)

    for thread in threads:
        thread.join()

    while not thread_results_queue.empty():
        stream_list.append(thread_results_queue.get())

    if len(stream_list) == 0:
        return []

    if config['debrid']:
        stream_list = sorted(stream_list, key=filter_by_availability)
        stream_list = sorted(stream_list, key=filter_by_direct_torrnet)
    return stream_list