1#!/usr/bin/python3 -OO
2# Copyright 2007-2021 The SABnzbd-Team <team@sabnzbd.org>
3#
4# This program is free software; you can redistribute it and/or
5# modify it under the terms of the GNU General Public License
6# as published by the Free Software Foundation; either version 2
7# of the License, or (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18import os
19from collections import namedtuple
20from re import compile
21
22CONFIG_VERSION = 19
23
24QUEUE_VERSION = 10
25POSTPROC_QUEUE_VERSION = 2
26
27REC_RAR_VERSION = 550
28
29PNFO = namedtuple(
30    "PNFO",
31    "repair unpack delete script nzo_id filename labels password unpackstrht "
32    "msgid category url bytes_left bytes avg_stamp avg_date finished_files "
33    "active_files queued_files status priority bytes_missing direct_unpack",
34)
35
36QNFO = namedtuple("QNFO", "bytes bytes_left bytes_left_previous_page list q_size_list q_fullsize")
37
38ANFO = namedtuple("ANFO", "article_sum cache_size cache_limit")
39
40# Leave some space for "_UNPACK_" which we append during post-proc
41# Or, when extra ".1", ".2" etc. are added for identically named jobs
42DEF_FOLDER_MAX = 256 - 10
43DEF_FILE_MAX = 255 - 10  # max filename length on modern filesystems, minus some room for extra chars later on
44
45GIGI = float(2 ** 30)
46MEBI = float(2 ** 20)
47KIBI = float(2 ** 10)
48
49BYTES_FILE_NAME_OLD = "totals9.sab"
50BYTES_FILE_NAME = "totals10.sab"
51QUEUE_FILE_TMPL = "queue%s.sab"
52QUEUE_FILE_NAME = QUEUE_FILE_TMPL % QUEUE_VERSION
53POSTPROC_QUEUE_FILE_NAME = "postproc%s.sab" % POSTPROC_QUEUE_VERSION
54RSS_FILE_NAME = "rss_data.sab"
55SCAN_FILE_NAME = "watched_data2.sab"
56RATING_FILE_NAME = "Rating.sab"
57FUTURE_Q_FOLDER = "future"
58JOB_ADMIN = "__ADMIN__"
59VERIFIED_FILE = "__verified__"
60RENAMES_FILE = "__renames__"
61ATTRIB_FILE = "SABnzbd_attrib"
62REPAIR_REQUEST = "repair-all.sab"
63
64SABYENC_VERSION_REQUIRED = "4.0.0"
65
66DB_HISTORY_VERSION = 1
67DB_HISTORY_NAME = "history%s.db" % DB_HISTORY_VERSION
68
69DEF_DOWNLOAD_DIR = os.path.normpath("Downloads/incomplete")
70DEF_COMPLETE_DIR = os.path.normpath("Downloads/complete")
71DEF_ADMIN_DIR = "admin"
72DEF_NZBBACK_DIR = ""
73DEF_LANGUAGE = "locale"
74DEF_INTERFACES = "interfaces"
75DEF_EMAIL_TMPL = "email"
76DEF_STDCONFIG = "Config"
77DEF_STDINTF = "Glitter"
78DEF_SKIN_COLORS = {"Glitter": "Auto", "plush": "gold"}
79DEF_MAIN_TMPL = os.path.normpath("templates/main.tmpl")
80DEF_INI_FILE = "sabnzbd.ini"
81DEF_HOST = "127.0.0.1"
82DEF_PORT = 8080
83DEF_WORKDIR = "sabnzbd"
84DEF_LOG_FILE = "sabnzbd.log"
85DEF_LOG_ERRFILE = "sabnzbd.error.log"
86DEF_LOG_CHERRY = "cherrypy.log"
87DEF_ARTICLE_CACHE_DEFAULT = "500M"
88DEF_ARTICLE_CACHE_MAX = "1G"
89DEF_TIMEOUT = 60
90DEF_SCANRATE = 5
91MAX_WARNINGS = 20
92MAX_BAD_ARTICLES = 5
93
94# Constants affecting download performance
95MIN_DECODE_QUEUE = 10
96LIMIT_DECODE_QUEUE = 100
97DIRECT_WRITE_TRIGGER = 35
98MAX_ASSEMBLER_QUEUE = 5
99
100REPAIR_PRIORITY = 3
101FORCE_PRIORITY = 2
102HIGH_PRIORITY = 1
103NORMAL_PRIORITY = 0
104LOW_PRIORITY = -1
105DEFAULT_PRIORITY = -100
106PAUSED_PRIORITY = -2
107DUP_PRIORITY = -3
108STOP_PRIORITY = -4
109
110INTERFACE_PRIORITIES = {
111    FORCE_PRIORITY: "Force",
112    REPAIR_PRIORITY: "Repair",
113    HIGH_PRIORITY: "High",
114    NORMAL_PRIORITY: "Normal",
115    LOW_PRIORITY: "Low",
116}
117
118STAGES = {"Source": 0, "Download": 1, "Servers": 2, "Repair": 3, "Filejoin": 4, "Unpack": 5, "Script": 6}
119
120VALID_ARCHIVES = (".zip", ".rar", ".7z")
121VALID_NZB_FILES = (".nzb", ".gz", ".bz2")
122
123CHEETAH_DIRECTIVES = {"directiveStartToken": "<!--#", "directiveEndToken": "#-->", "prioritizeSearchListOverSelf": True}
124
125IGNORED_FOLDERS = ("@eaDir", ".appleDouble")
126
127# (MATCHER, [EXTRA, MATCHERS])
128series_match = [
129    (compile(r"( [sS]|[\d]+)x(\d+)"), [compile(r"^[-\.]+([sS]|[\d])+x(\d+)"), compile(r"^[-\.](\d+)")]),  # 1x01
130    (
131        compile(r"[Ss](\d+)[\.\-]?[Ee](\d+)"),  # S01E01
132        [compile(r"^[-\.]+[Ss](\d+)[\.\-]?[Ee](\d+)"), compile(r"^[-\.](\d+)")],
133    ),
134    (compile(r"[ \-_\.](\d)(\d{2,2})[ \-_\.]"), []),  # .101. / _101_ / etc.
135    (compile(r"[ \-_\.](\d)(\d{2,2})$"), []),  # .101 at end of title
136]
137
138date_match = [r"(\d{4})\W(\d{1,2})\W(\d{1,2})", r"(\d{1,2})\W(\d{1,2})\W(\d{4})"]  # 2008-10-16  # 10.16.2008
139
140year_match = r"[\W]([1|2]\d{3})([^\w]|$)"  # Something '(YYYY)' or '.YYYY.' or ' YYYY '
141
142sample_match = r"((^|[\W_])(sample|proof))"  # something-sample or something-proof
143
144resolution_match = r"(^|[\W_])((240|360|480|540|576|720|900|1080|1440|2160|4320)[piP])([\W_]|$)"  # 576i, 720p, 1080P
145
146
147class Status:
148    COMPLETED = "Completed"  # PP: Job is finished
149    CHECKING = "Checking"  # Q:  Pre-check is running
150    DOWNLOADING = "Downloading"  # Q:  Normal downloading
151    EXTRACTING = "Extracting"  # PP: Archives are being extracted
152    FAILED = "Failed"  # PP: Job has failed, now in History
153    FETCHING = "Fetching"  # Q:  Job is downloading extra par2 files
154    GRABBING = "Grabbing"  # Q:  Getting an NZB from an external site
155    MOVING = "Moving"  # PP: Files are being moved
156    PAUSED = "Paused"  # Q:  Job is paused
157    QUEUED = "Queued"  # Q:  Job is waiting for its turn to download or post-process
158    QUICK_CHECK = "QuickCheck"  # PP: QuickCheck verification is running
159    REPAIRING = "Repairing"  # PP: Job is being repaired (by par2)
160    RUNNING = "Running"  # PP: User's post processing script is running
161    VERIFYING = "Verifying"  # PP: Job is being verified (by par2)
162    DELETED = "Deleted"  # Q:  Job has been deleted (and is almost gone)
163    PROP = "Propagating"  # Q:  Delayed download
164