-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp_restore_backup.py
151 lines (115 loc) · 4.7 KB
/
app_restore_backup.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
"""
This script can restore a backup to another Emby server from previously using app_backup.py.
The script assumes that the new Emby server does not have the same item ID as the old Emby server and
will lookup all items by IMDB or TVDB ID.
Execute the script from the command line with the required arguments:
--host: The Emby server host.
--user_id: The user ID for the Emby server (ID string, not the user name).
--api_key: The API key for accessing the Emby server.
--source_file: The path to the backup file.
You can use this if you don't have access to the Emby database but you do have access to the API.
Example command:
python3 restore_backup.py -host http://xxx.xxx.xxx.xxx:xxxx -user_id abc123 -api_key abc123 -source_file "backup\IsPlayed_SomeUsername.json"
Json file exmple:
{
"user_name": "Jason",
"user_id": "abc123",
"filter": "IsPlayed", // or "IsFavorite"
"Items": [
{
"Id": "15172942",
"Name": "The Matrix",
"ProviderIds": {
"Imdb": "tt21215388",
"Tmdb": "1090446",
"Tvdb": "357485"
},
"Type": "Movie"
},
etc
"""
from src.emby import Emby
import ast
import sys
from argparse import ArgumentParser
backup_filters = ["IsPlayed", "IsFavorite"]
def get_provider_id(provider_ids, key):
# Normalize keys to lowercase since some providers return keys in different cases
normalized_dict = {k.lower(): v for k, v in provider_ids.items()}
return normalized_dict.get(key.lower())
def add_error(error):
print(f"\nERROR: {error}")
def main(args):
emby = Emby(args.host, args.user_id, args.api_key)
emby.seconds_between_requests = 0.1
backup_file = args.source_file
with open(backup_file, "r") as file:
backup_data = file.read()
filter = ast.literal_eval(backup_data)["filter"]
items = ast.literal_eval(backup_data)["Items"]
user_id = args.user_id
for item in items:
if "Id" not in item or "Name" not in item:
add_error(f"{item} is missing Id or Name")
continue
item_id = int(item["Id"])
new_item_ids = None
item_name = item["Name"]
item_type = item["Type"]
imdb_id = None
tvdb_id = None
provider_ids = item.get("ProviderIds", None)
if provider_ids is None:
add_error(f"{item_id}:{item_name} is missing ProviderIds")
continue
imdb_id = get_provider_id(provider_ids, "imdb")
tvdb_id = get_provider_id(provider_ids, "tvdb")
if imdb_id is None and tvdb_id is None:
add_error(f"Can not find IMDB or TVDB ID for: {item_id}: {item_name}")
continue
if item_type not in ["Series", "Episode", "Movie"]:
add_error(f"{item_id}:{item_name} has invalid type {item_type}")
continue
if item_type == "Episode":
if tvdb_id is None:
add_error(f"Can not find TVDB ID for episode {item_id}: {item_name}")
continue
new_item_ids = emby.get_items_with_tvdb_id([tvdb_id], [item_type])
elif item_type == "Series" or item_type == "Movie":
if imdb_id is not None: # Prefer IMDB first I guess
new_item_ids = emby.get_items_with_imdb_id([imdb_id], [item_type])
elif tvdb_id is not None:
new_item_ids = emby.get_items_with_tvdb_id([tvdb_id], [item_type])
if new_item_ids is None or new_item_ids == []:
add_error(
f"Can not find new Item for TVDB: {tvdb_id} IMDB: {imdb_id} Name: {item_name}. "
)
continue
for new_item_id in new_item_ids:
if filter == "IsPlayed":
set_as_played = emby.set_item_as_played(user_id, new_item_id)
if not set_as_played:
add_error(f"Cannot set to IsPlayed {new_item_id}: {item_name}")
elif filter == "IsFavorite":
set_as_favorite = emby.set_item_as_favorite(user_id, new_item_id)
if not set_as_favorite:
add_error(f"Cannot set to IsFavorite {item_id}: {item_name}")
if __name__ == "__main__":
parser = ArgumentParser()
if len(sys.argv) == 1:
parser.print_help()
quit()
parser.add_argument("-host", dest="host", help="Destination Emby host")
parser.add_argument("-user_id", dest="user_id", help="Destination user ID")
parser.add_argument(
"-api_key",
dest="api_key",
help="Destination Emby API key",
)
parser.add_argument(
"-source_file",
dest="source_file",
help="Source file to restore from",
)
args = parser.parse_args()
main(args)