-
Notifications
You must be signed in to change notification settings - Fork 0
/
refreshEps.py
119 lines (93 loc) · 4.09 KB
/
refreshEps.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
import asyncio
import importlib
import logging
from datetime import datetime, timedelta
from os import path
from queue import Queue
from time import sleep
import requests
from db import theSqliteDict
from helpers import (get_current_title_by_id, get_db, has_placeholder_thumb,
is_placeholder_title, logging_setup)
dir = path.split(path.abspath(__file__))[0]
# import config
conf = importlib.import_module('config')
if path.isfile(f'{dir}/config_local.py'):
conf = importlib.import_module('config_local')
api_token = conf.api_token
base_url = conf.base_url
days_before_giving_up = conf.days_before_giving_up
logging_level = conf.logging_level
recheck_wait_time = conf.recheck_wait_time
limit_concurrent_requests = conf.limit_concurrent_requests
logging_setup(__file__)
api_json = {"X-Emby-Token": api_token}
headers={"user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/81.0.4044.138 safari/537.36"}
headers.update(api_json)
async def refresh_ep(ep:object, episodes:theSqliteDict):
item_id = ep['id']
checked_since = datetime.strptime(ep['checked_since'], '%Y-%m-%d %H:%M')
# get current name of item
current_title, series_name = get_current_title_by_id(item_id)
if current_title == None and series_name == None:
logging.error(f'DELETING item {item_id} - Item doesnt exist on Emby')
episodes._remove(item_id)
return
needs_title = is_placeholder_title(current_title)
needs_thumb = has_placeholder_thumb(item_id)
if not needs_title and not needs_thumb:
# does not have dummy episode title, dont write it back to the file
logging.warning(f'DELETING item {item_id} - {series_name} - {current_title} - Thumb and title already good')
episodes._remove(item_id)
return
# has filler episode title, refresh it
logging.warning(f'REFRESHING item {item_id} - {series_name} - {current_title} needs:{" thumb" if needs_thumb else ""}{" title" if needs_title else ""}')
raw_data = {
'Id': item_id,
'ReplaceAllMetadata': True,
'ReplaceAllImages': True
}
res = requests.post(f'{base_url}/Items/{item_id}/Refresh', params=raw_data, headers=headers)
if(res.status_code < 400):
logging.debug(f'Successfully refreshed ID {item_id}')
else:
logging.critical(f'Something went wrong refreshing {item_id}! Returned {res.status_code} - {res.text}')
# wait then check if name has changed
sleep(recheck_wait_time)
logging.debug(f'Checking name of {item_id} to see if it changed.')
current_title, series_name = get_current_title_by_id(item_id)
needs_title = is_placeholder_title(current_title)
needs_thumb = has_placeholder_thumb(item_id)
if not needs_title and not needs_thumb:
# does not have dummy episode title, dont write it back to the file
logging.warning(f'DELETING item {item_id} - {series_name} - {current_title} - Thumb and title good')
episodes._remove(item_id)
return
if datetime.now() - checked_since >= timedelta(days=days_before_giving_up):
logging.warning(f'GIVING UP on {item_id} - {series_name} - {current_title} - needs:{" thumb" if needs_thumb else ""}{" title" if needs_title else ""}')
episodes._remove(item_id)
return
logging.info(f'Item {item_id} - {series_name} - {current_title} - needs:{" thumb" if needs_thumb else ""}{" title" if needs_title else ""}')
episodes._update({
'series': series_name,
'last_title': current_title,
'needs_title': needs_title,
'needs_thumb': needs_thumb
}, item_id)
async def main():
db = get_db()
try:
ps = Queue()
for ep in db._all():
while ps.qsize() > limit_concurrent_requests:
logging.debug('Queue too big! Waiting for some to finish')
await ps.get()
ps.put(asyncio.create_task(refresh_ep(ep, db)))
while not ps.empty():
await ps.get()
except:
pass
finally:
db.close()
if __name__ == '__main__':
asyncio.run(main())