This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtracker.py
178 lines (132 loc) · 5.2 KB
/
tracker.py
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
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
from typing import Optional, List
from enum import Enum, auto
import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from os.path import isfile
from json import loads
# https://github.com/ArchiveTeam/tencent-weibo-grab/blob/9bae5f9747e014db9227821a9c11557267967023/pipeline.py
VERSION = "20201017.01"
TRACKER_ID = "ext-yt-communitycontribs"
TRACKER_HOST = "trackerproxy.meo.ws"
BACKFEED_HOST = "blackbird-amqp.meo.ws:23038"
BACKFEED_ENDPOINT = f"http://{BACKFEED_HOST}/{TRACKER_ID}-kj57sxhhzcn2kqjp/"
TRACKER_ENDPOINT = f"http://{TRACKER_HOST}/{TRACKER_ID}"
from os import environ
if "TRACKER_USERNAME" in environ.keys():
TRACKER_USERNAME = environ["TRACKER_USERNAME"]
elif isfile("config.json"):
try:
TRACKER_USERNAME = loads(open("config.json").read())["TRACKER_USERNAME"]
except:
TRACKER_USERNAME = "Unnamed"
else:
TRACKER_USERNAME = "Unnamed"
# https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
retry_strategy = Retry(
total=4,
backoff_factor=2,
status_forcelist=[x for x in range(500, 600)] + [429],
method_whitelist=["GET", "POST"]
)
adapter = HTTPAdapter(max_retries=retry_strategy)
tracker_session = requests.Session()
tracker_session.mount("https://", adapter)
tracker_session.mount("http://", adapter)
class ItemType(Enum):
Video = auto()
Channel = auto()
MixPlaylist = auto()
Playlist = auto()
def add_item_to_tracker(item_type: ItemType, item_id: str) -> bool:
"""Feed items into the tracker through backfeed (item names will be deduplicated):
# curl -d 'ITEMNAME' -so/dev/null $amqp_endpoint
# Response codes:
# 200 - Item added to tracker
# 409 - Item is already in tracker
# 404 - Project backfeed channel not found
# 400 - Item name has a bad format
"""
type_name = item_type.name.lower()
item_name = f"{type_name}:{item_id}"
req = tracker_session.post(BACKFEED_ENDPOINT, data=item_name)
code = req.status_code
if code == 200:
# print(f"[INFO] Item ID \'{item_name}\' added to tracker successfully")
return True
elif code == 409:
# print(f"[INFO] Item ID \'{item_name}\' has already been added to tracker")
return True
elif code == 404:
print(f"[ERROR] Unable to add item ID \'{item_name}\' to tracker. Project backfeed channel not found: {BACKFEED_ENDPOINT}")
elif code == 400:
print(f"[ERROR] Item ID \'{item_name}\' has a bad format")
else:
print(f"[ERROR] Unknown response code adding item \'{item_name}\' to tracker: {code}")
return False
def request_item_from_tracker() -> Optional[str]:
data = {
"downloader": TRACKER_USERNAME,
"api_version": "2",
"version": VERSION
}
req = tracker_session.post(f"{TRACKER_ENDPOINT}/request", json=data)
code = req.status_code
if code == 200:
data = req.json()
if "item_name" in data:
item_name = data["item_name"]
print(f"[INFO] Received an item from tracker: {item_name}")
return item_name
else:
print(f"[ERROR] Received item is missing the \'item_name\' key: {data}")
else:
print(f"[ERROR] Unable to get an item from tracker. Status: {code}")
def request_upload_target() -> Optional[str]:
req = tracker_session.get(f"{TRACKER_ENDPOINT}/upload")
code = req.status_code
if code == 200:
data = req.json()
if "upload_target" in data:
upload_target = data["upload_target"]
print(f"[INFO] Received an upload target from tracker: {upload_target}")
return upload_target
else:
print(f"[ERROR] Response is missing the \'upload_target\' key: {data}")
else:
print(f"[ERROR] Unable to get an upload target from tracker. Status: {code}")
def request_all_upload_targets() -> Optional[List[str]]:
req = tracker_session.get(f"{TRACKER_ENDPOINT}/upload_targets")
code = req.status_code
if code == 200:
data = req.json()
print(f"[INFO] Received all upload targets from tracker: {data}")
return data
else:
print(f"[ERROR] Unable to get all upload targets from tracker. Status: {code}")
# `item_name` includes type prefix (video:id, playlist:id, etc)
def mark_item_as_done(item_name: str, item_size_bytes: int) -> bool:
data = {
"downloader": TRACKER_USERNAME,
"version": VERSION,
"item": item_name,
"bytes": {
"data": item_size_bytes
}
}
req = tracker_session.post(f"{TRACKER_ENDPOINT}/done", json=data)
code = req.status_code
if code == 200:
print(f"[INFO] Marked item \'{item_name}\' as done")
return True
elif code > 399 and code < 500:
print(f"[ERROR] Unable to mark item as done. Status: {code}")
else:
print(f"[ERROR] Unknown response code while marking item \'{item_name}\' as done: {code}")
return False
# if __name__ == "__main__":
# print(add_item_to_tracker(ItemType.Channel, "test10"))
# print(request_item_from_tracker())
# print(request_upload_target())
# print(request_all_upload_targets())
# print(mark_item_as_done("test4", 200))