-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoverseerr-migration-script.py
More file actions
executable file
·912 lines (757 loc) · 36.4 KB
/
overseerr-migration-script.py
File metadata and controls
executable file
·912 lines (757 loc) · 36.4 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#!/usr/bin/env python3
# Python script to migrate Overseerr to Jellyseerr
# https://github.com/Quack6765/seerr-migration-script
import sys
import time
import argparse
import requests
import json
import logging
from typing import List, Dict, Any, Optional
from requests.exceptions import RequestException
# Setup logging
logging.basicConfig(
level=logging.INFO, # Default to INFO level
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('migration.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Add command-line arguments
def parse_args():
parser = argparse.ArgumentParser()
# Add source argument
parser.add_argument('-s', '--source', help='The URL source from which to retrieve data (Overseerr)')
# Add source API key argument
parser.add_argument('-k', '--source_api_key', help='The API Key for the source (Overseerr)')
# Add target argument
parser.add_argument('-t', '--target', help='The URL target where to send data (Jellyseerr)')
# Add target API key argument
parser.add_argument('-a', '--target_api_key', help='The API Key for the target (Jellyseerr)')
# Add TMDB API key argument (optional)
parser.add_argument('-m', '--tmdb_api_key', help='The API Key for TMDB (optional)', required=False)
# Add debug flag
parser.add_argument('-d', '--debug', action='store_true', help='Enable debug logging')
args = parser.parse_args()
# Set logging level based on debug flag
if args.debug:
logger.setLevel(logging.DEBUG)
logger.debug("Debug logging enabled")
return args
def main() -> int:
"""Main entry point for the migration script.
Returns:
int: Exit code (0 for success, 1 for failure)
"""
try:
args = parse_args()
if not all([args.source, args.source_api_key, args.target, args.target_api_key]):
logger.error("All arguments must be provided and cannot be empty.")
return 1
global SOURCE_URL
global SOURCE_APIKEY
global TARGET_URL
global TARGET_APIKEY
global TMDB_APIKEY
SOURCE_URL = f"{args.source.rstrip('/')}/api/v1"
SOURCE_APIKEY = args.source_api_key
TARGET_URL = f"{args.target.rstrip('/')}/api/v1"
TARGET_APIKEY = args.target_api_key
TMDB_APIKEY = args.tmdb_api_key or ''
logger.info("Starting migration process...")
success = migration()
if success:
logger.info("Migration completed successfully!")
return 0
else:
logger.warning("Migration completed with errors. Check the logs for details.")
return 1
except KeyboardInterrupt:
logger.info("\nMigration interrupted by user")
return 1
except Exception as e:
logger.error(f"Migration failed: {str(e)}", exc_info=True)
return 1
def fetch_data(url: str, api_key: str, endpoint: str, params: Dict[str, Any]) -> List[Dict[str, Any]]:
"""Fetch data from API endpoint with error handling.
Args:
url: Base URL for the API
api_key: API key for authentication
endpoint: API endpoint to fetch from
params: Query parameters
Returns:
List of results from the API
Raises:
RequestException: If the API request fails
"""
# Initialize response variable before try block
r = None
try:
r = requests.get(
url=f"{url}{endpoint}",
headers={"X-Api-Key": api_key},
params=params,
timeout=30
)
r.raise_for_status()
response_data = r.json()
# Check if the response has a 'results' field
if 'results' not in response_data:
logger.error(f"Response from {endpoint} does not contain 'results' field. Response: {json.dumps(response_data)}")
return []
return response_data["results"]
except RequestException as e:
# Get the response content if available
response_text = ""
if r and hasattr(r, 'text'):
try:
response_text = f" Response: {r.text}"
except:
pass
logger.error(f"Failed to fetch data from {endpoint}: {str(e)}{response_text}")
raise
def migration() -> bool:
"""Main migration function to transfer users and requests.
Returns:
bool: True if migration was successful, False if there were any errors
"""
try:
if not testConnections():
logger.error("Connection test failed")
return False
# Get list of current users and requests
global SOURCE_USERS, TARGET_USERS, SOURCE_REQUESTS, TARGET_REQUESTS
logger.info("Fetching users and requests from both systems...")
try:
SOURCE_USERS = fetch_data(SOURCE_URL, SOURCE_APIKEY, "/user", {"take": 500})
TARGET_USERS = fetch_data(TARGET_URL, TARGET_APIKEY, "/user", {"take": 500})
SOURCE_REQUESTS = fetch_data(SOURCE_URL, SOURCE_APIKEY, "/request", {"take": 1000, "filter": "unavailable"})
TARGET_REQUESTS = fetch_data(TARGET_URL, TARGET_APIKEY, "/request", {"take": 1000, "filter": "unavailable"})
except Exception as e:
logger.error(f"Failed to fetch initial data: {str(e)}")
return False
# Migrate users
logger.info(f"Starting migration of {len(SOURCE_USERS)} users...")
success_count = 0
failure_count = 0
for user in SOURCE_USERS:
try:
if migrateUser(user):
success_count += 1
else:
failure_count += 1
except Exception as e:
logger.error(f"Unexpected error migrating user: {str(e)}")
failure_count += 1
continue # Continue with next user
total = len(SOURCE_USERS)
logger.info(f"Migration completed. Success: {success_count}/{total} ({success_count/total*100:.1f}%), Failures: {failure_count}/{total} ({failure_count/total*100:.1f}%)")
return failure_count == 0
except Exception as e:
logger.error(f"Migration failed: {str(e)}", exc_info=True)
return False
def testConnections() -> bool:
"""Test connections to both Overseerr and Jellyseerr servers.
Returns:
bool: True if both connections succeed, False otherwise
"""
# Test Overseerr connection
status = "Testing Overseerr connection ... "
print(status, end="", flush=True)
r = None
try:
r = requests.get(
url=SOURCE_URL+"/settings/main",
headers={"X-Api-Key": SOURCE_APIKEY},
timeout=10
)
r.raise_for_status()
print("OK")
except requests.exceptions.RequestException as e:
# Get the response content if available
response_text = ""
if r and hasattr(r, 'text'):
try:
response_text = f" Response: {r.text}"
except:
pass
logger.error(f"Couldn't connect to Overseerr! {str(e)}{response_text}")
return False
# Test Jellyseerr connection
status = "Testing Jellyseerr connection ... "
print(status, end="", flush=True)
r = None
try:
r = requests.get(
url=TARGET_URL+"/settings/main",
headers={"X-Api-Key": TARGET_APIKEY},
timeout=10
)
r.raise_for_status()
print("OK")
except requests.exceptions.RequestException as e:
# Get the response content if available
response_text = ""
if r and hasattr(r, 'text'):
try:
response_text = f" Response: {r.text}"
except:
pass
logger.error(f"Couldn't connect to Jellyseerr! {str(e)}{response_text}")
return False
return True
def migrateUser(user: Dict[str, Any]) -> bool:
"""Migrate a single user from Overseerr to Jellyseerr.
Args:
user: User data dictionary from Overseerr
Returns:
bool: True if migration was successful, False otherwise
"""
try:
email = user.get("email")
if not email:
logger.error("User data missing email field")
return False
logger.info(f"Processing user: {email}")
# Check if user already exists - use the new function to get detailed user info
existing_user = fetch_user_by_email(email)
if existing_user:
userNewID = existing_user["id"]
user_source = existing_user.get("userType", "unknown")
display_name = existing_user.get("displayName", "unknown")
logger.info(f"User '{email}' already exists in Jellyseerr with ID {userNewID}, source: {user_source}, display name: {display_name}")
logger.debug(f"Existing user details: {json.dumps(existing_user)}")
# Check if user has request permissions
permissions = existing_user.get("permissions", 0)
if (permissions & 1) != 1:
logger.info(f"Adding request permission to existing user '{email}'")
permissions |= 1 # Set bit 1 (request permission)
# Update permissions
r = requests.put(
url=f"{TARGET_URL}/user/{userNewID}",
headers={"X-Api-Key": TARGET_APIKEY},
json={"permissions": permissions},
timeout=30
)
r.raise_for_status()
logger.info(f"Updated permissions for user '{email}' to {permissions}")
else:
logger.info(f"User '{email}' already has request permissions")
else:
# Before creating a new user, check if there's a Jellyfin user with the same email
# This requires fetching all users and checking their details
logger.info(f"No exact match found for user '{email}', checking for Jellyfin users with the same email")
# Get all users
all_users = fetch_data(TARGET_URL, TARGET_APIKEY, "/user", {"take": 500})
# Check for any user with the same email, regardless of source
jellyfin_user = None
for u in all_users:
if u.get("email") == email:
# Get detailed user info
user_detail = fetch_user_by_email(email)
if user_detail and user_detail.get("userType") == "jellyfin":
jellyfin_user = user_detail
break
if jellyfin_user:
userNewID = jellyfin_user["id"]
logger.info(f"Found Jellyfin user with email '{email}', ID: {userNewID}. Using this user instead of creating a new one.")
# Update permissions if needed
permissions = jellyfin_user.get("permissions", 0)
if (permissions & 1) != 1:
logger.info(f"Adding request permission to Jellyfin user '{email}'")
permissions |= 1 # Set bit 1 (request permission)
# Update permissions
r = requests.put(
url=f"{TARGET_URL}/user/{userNewID}",
headers={"X-Api-Key": TARGET_APIKEY},
json={"permissions": permissions},
timeout=30
)
r.raise_for_status()
logger.info(f"Updated permissions for Jellyfin user '{email}' to {permissions}")
else:
# Create new user
try:
newUsername = user.get("username") or user.get("plexUsername")
if not newUsername:
logger.error(f"User '{email}' missing both username and plexUsername")
return False
payload = {
"email": email,
"username": newUsername,
"permissions": user.get("permissions", 0) # Default to 0 permissions if not specified
}
r = None
try:
r = requests.post(
url=f"{TARGET_URL}/user",
headers={"X-Api-Key": TARGET_APIKEY},
json=payload,
timeout=30
)
r.raise_for_status()
userNewID = r.json()["id"]
# Update permissions - ensure user has request permissions (bit 1)
permissions = user.get("permissions", 0)
# Set bit 1 (request permission) if not already set
if (permissions & 1) != 1:
logger.info(f"Adding request permission to user '{email}'")
permissions |= 1 # Set bit 1 (request permission)
r = requests.put(
url=f"{TARGET_URL}/user/{userNewID}",
headers={"X-Api-Key": TARGET_APIKEY},
json={"permissions": permissions},
timeout=30
)
r.raise_for_status()
logger.info(f"User '{email}' created in Jellyseerr with permissions: {permissions}")
except RequestException as e:
logger.error(f"Failed to create/update user '{email}': {str(e)} - Response: {r.text if r and hasattr(r, 'text') else 'No response text'}")
return False
except Exception as e:
logger.error(f"Failed to prepare user data for '{email}': {str(e)}")
return False
# Get source user ID
userOldID = next(
(u["id"] for u in SOURCE_USERS if u["email"] == email),
None
)
if not userOldID:
logger.error(f"Could not find source user ID for email: {email}")
return False
# Handle notifications and requests
try:
# Handle notifications and requests
notifications_disabled = change_jellyseerr_user_notifications(TARGET_URL, TARGET_APIKEY, userNewID, "disable")
if not notifications_disabled:
logger.warning(f"Failed to disable notifications for user '{email}', continuing anyway")
requests_success = migrateRequests(userOldID, userNewID)
notifications_enabled = change_jellyseerr_user_notifications(TARGET_URL, TARGET_APIKEY, userNewID, "enable")
if not notifications_enabled:
logger.warning(f"Failed to enable notifications for user '{email}', continuing anyway")
# Verify all requests for this user after migration
logger.info(f"Verifying all requests for user '{email}' (ID: {userNewID}) in Jellyseerr after migration")
jellyseerr_requests = fetch_user_requests(userNewID)
if jellyseerr_requests:
logger.info(f"Found {len(jellyseerr_requests)} requests for user '{email}' in Jellyseerr")
for req in jellyseerr_requests:
media_type = req["media"]["mediaType"]
tmdb_id = req["media"]["tmdbId"]
title = req["media"].get("title", req["media"].get("name", "Unknown"))
logger.info(f"Request: {media_type} '{title}' (tmdbId:{tmdb_id})")
else:
logger.warning(f"No requests found for user '{email}' in Jellyseerr after migration")
# Consider migration successful if requests were migrated successfully
# Notification failures are treated as warnings only
return requests_success
except Exception as e:
logger.error(f"Failed to handle notifications/requests for user '{email}': {str(e)}")
return False
except Exception as e:
logger.error(f"Failed to migrate user '{user.get('email', 'unknown')}': {str(e)}")
return False
def change_jellyseerr_user_notifications(jellyseerr_url: str, jellyseerr_api_key: str, user_id: int, change_type: str) -> Optional[Dict[str, Any]]:
"""Change notification settings for a Jellyseerr user.
Args:
jellyseerr_url: Base URL for Jellyseerr API
jellyseerr_api_key: API key for authentication
user_id: ID of the user to modify
change_type: Either 'enable' or 'disable'
Returns:
Optional[Dict[str, Any]]: Response from the API if successful, None if failed
Raises:
ValueError: If change_type is invalid
RequestException: If the API request fails
"""
if change_type not in ["enable", "disable"]:
raise ValueError(f"Invalid change_type: {change_type}")
headers = {
"X-Api-Key": jellyseerr_api_key,
"Content-Type": "application/json"
}
url = f"{jellyseerr_url}/user/{user_id}/settings/notifications"
notification_payload_value = 3661 if change_type == "enable" else 0
notification_payload = {
"notificationTypes": {
"discord": notification_payload_value,
"email": notification_payload_value,
"pushbullet": notification_payload_value,
"pushover": notification_payload_value,
"slack": notification_payload_value,
"telegram": notification_payload_value,
"webhook": notification_payload_value,
"webpush": notification_payload_value
}
}
# Initialize response variable before try block
response = None
try:
logger.debug(f"Sending {change_type} notifications request for user {user_id} to {url}")
logger.debug(f"Notification payload: {json.dumps(notification_payload)}")
response = requests.post(
url,
headers=headers,
json=notification_payload,
timeout=30
)
response.raise_for_status()
response_data = response.json()
logger.debug(f"Notification {change_type} response: {json.dumps(response_data)}")
return response_data
except RequestException as e:
logger.error(f"Failed to {change_type} notifications for user {user_id}: {str(e)} - Response: {response.text if response and hasattr(response, 'text') else 'No response text'}")
raise
def create_request_payload(request: Dict[str, Any], user_id: int) -> Dict[str, Any]:
"""Create a request payload for Jellyseerr API.
Args:
request: Source request data from Overseerr
user_id: Target user ID in Jellyseerr
Returns:
Dict[str, Any]: Request payload for Jellyseerr API
"""
is4k = request["is4k"]
media_type = request["media"]["mediaType"]
tmdb_id = request["media"]["tmdbId"]
logger.debug(f"Creating request payload for {media_type} (tmdbId:{tmdb_id}) with user ID {user_id}")
# Ensure user_id is an integer
if not isinstance(user_id, int):
logger.warning(f"User ID {user_id} is not an integer, converting to int")
try:
user_id = int(user_id)
except (ValueError, TypeError):
logger.error(f"Failed to convert user ID {user_id} to integer")
# Raise an exception instead of defaulting to admin user
raise ValueError(f"Invalid user ID: {user_id}")
logger.debug(f"Using user ID {user_id} for request")
payload = {
"mediaType": media_type,
"mediaId": tmdb_id,
"tmdbId": tmdb_id,
"is4k": is4k,
"userId": user_id,
"sendNotification": False
}
if media_type == "tv":
payload["seasons"] = [season["seasonNumber"] for season in request["seasons"]]
logger.debug(f"Adding seasons to request: {payload['seasons']}")
logger.debug(f"Final request payload: {json.dumps(payload)}")
return payload
def fetch_tmdb_media_details(tmdb_id: int, media_type: str) -> Dict[str, Any]:
"""Fetch media details from TMDB API.
Args:
tmdb_id: TMDB ID of the media
media_type: Type of media ('movie' or 'tv')
Returns:
Dict containing media details
"""
try:
endpoint = 'movie' if media_type == 'movie' else 'tv'
r = requests.get(
f"https://api.themoviedb.org/3/{endpoint}/{tmdb_id}",
params={"api_key": TMDB_APIKEY}
)
r.raise_for_status()
return r.json()
except RequestException as e:
logger.error(f"Failed to fetch TMDB details for {media_type} with ID {tmdb_id}: {str(e)}")
return {}
def is_request_exists(request: Dict[str, Any], target_requests: List[Dict[str, Any]]) -> bool:
"""Check if a request already exists in Jellyseerr.
Args:
request: Source request from Overseerr
target_requests: List of existing requests in Jellyseerr
Returns:
bool: True if request exists, False otherwise
"""
for target_request in target_requests:
if (request["media"]["tmdbId"] == target_request["media"]["tmdbId"] and
request["is4k"] == target_request["is4k"]):
if request["media"]["mediaType"] == "tv":
requested_seasons = set(season["seasonNumber"] for season in request["seasons"])
target_seasons = set(season["seasonNumber"] for season in target_request["seasons"])
if requested_seasons & target_seasons: # Check for any common seasons
return True
else:
return True
return False
def verify_request_created(request_id: int) -> bool:
"""Verify that a request was actually created in Jellyseerr by fetching it.
Args:
request_id: The ID of the request to verify
Returns:
bool: True if the request exists, False otherwise
"""
try:
logger.debug(f"Verifying request ID {request_id} exists in Jellyseerr")
r = requests.get(
url=f"{TARGET_URL}/request/{request_id}",
headers={"X-Api-Key": TARGET_APIKEY},
timeout=30
)
try:
r.raise_for_status()
# Log details about the request
request_data = r.json()
media_type = request_data.get("media", {}).get("mediaType", "unknown")
tmdb_id = request_data.get("media", {}).get("tmdbId", "unknown")
title = request_data.get("media", {}).get("title", request_data.get("media", {}).get("name", "Unknown"))
user_id = request_data.get("requestedBy", {}).get("id", "unknown")
status = request_data.get("status", "unknown")
logger.debug(f"Request details - ID: {request_id}, Type: {media_type}, Title: '{title}', TMDB ID: {tmdb_id}, User ID: {user_id}, Status: {status}")
return True
except requests.exceptions.HTTPError as e:
# Get the response content if available
response_text = ""
try:
response_text = f" Response: {r.text}"
except:
pass
logger.error(f"Failed to verify request ID {request_id}: {str(e)}{response_text}")
return False
except Exception as e:
# Get the response content if available
response_text = ""
if hasattr(e, 'response') and e.response:
try:
response_text = f" Response: {e.response.text}"
except:
pass
logger.error(f"Failed to verify request ID {request_id}: {str(e)}{response_text}")
return False
def verify_user_exists(user_id: int) -> bool:
"""Verify that a user exists in Jellyseerr by fetching their details.
Args:
user_id: The ID of the user to verify
Returns:
bool: True if the user exists, False otherwise
"""
try:
logger.info(f"Verifying user ID {user_id} (type: {type(user_id).__name__}) exists in Jellyseerr")
# Ensure user_id is an integer
if not isinstance(user_id, int):
logger.warning(f"User ID {user_id} is not an integer, attempting to convert")
try:
user_id = int(user_id)
logger.info(f"Successfully converted user ID to integer: {user_id}")
except (ValueError, TypeError) as e:
logger.error(f"Failed to convert user ID {user_id} to integer: {str(e)}")
return False
r = requests.get(
url=f"{TARGET_URL}/user/{user_id}",
headers={"X-Api-Key": TARGET_APIKEY},
timeout=30
)
r.raise_for_status()
user_data = r.json()
# Log detailed user information
email = user_data.get('email', 'unknown')
username = user_data.get('username', 'unknown')
display_name = user_data.get('displayName', 'unknown')
user_type = user_data.get('userType', 'unknown')
created_at = user_data.get('createdAt', 'unknown')
logger.info(f"User verified - ID: {user_id}, Email: {email}, Username: {username}, Display Name: {display_name}, Type: {user_type}")
logger.debug(f"Full user data: {json.dumps(user_data)}")
# Check if the user has the necessary permissions to make requests
permissions = user_data.get('permissions', 0)
logger.debug(f"User permissions: {permissions}")
# Check if user has request permissions (bit 1)
can_request = (permissions & 1) == 1
if not can_request:
logger.warning(f"User ID {user_id} does not have request permissions (permissions={permissions})")
return True
except Exception as e:
logger.error(f"Failed to verify user ID {user_id}: {str(e)}")
return False
def fetch_user_by_email(email: str) -> Optional[Dict[str, Any]]:
"""Fetch a user by email from Jellyseerr.
Args:
email: The email of the user to fetch
Returns:
Optional[Dict[str, Any]]: User data if found, None otherwise
"""
try:
logger.debug(f"Fetching user with email '{email}' from Jellyseerr")
# First, get all users
r = requests.get(
url=f"{TARGET_URL}/user",
headers={"X-Api-Key": TARGET_APIKEY},
params={"take": 500},
timeout=30
)
r.raise_for_status()
users_data = r.json()["results"]
# Find user with matching email
for user in users_data:
if user.get("email") == email:
user_id = user.get("id")
user_type = user.get("userType", "unknown")
logger.debug(f"Found user with email '{email}': ID {user_id}, Type: {user_type}")
# Get detailed user info
r = requests.get(
url=f"{TARGET_URL}/user/{user_id}",
headers={"X-Api-Key": TARGET_APIKEY},
timeout=30
)
r.raise_for_status()
user_data = r.json()
logger.debug(f"User details: {json.dumps(user_data)}")
return user_data
logger.debug(f"No user found with email '{email}'")
return None
except Exception as e:
logger.error(f"Failed to fetch user with email '{email}': {str(e)}")
return None
def fetch_user_requests(user_id: int) -> List[Dict[str, Any]]:
"""Fetch all requests for a specific user from Jellyseerr.
Args:
user_id: The ID of the user to fetch requests for
Returns:
List[Dict[str, Any]]: List of requests for the user
"""
try:
logger.debug(f"Fetching requests for user ID {user_id} from Jellyseerr")
r = requests.get(
url=f"{TARGET_URL}/request",
headers={"X-Api-Key": TARGET_APIKEY},
params={"take": 100, "requestedBy": user_id},
timeout=30
)
r.raise_for_status()
requests_data = r.json()["results"]
logger.debug(f"Found {len(requests_data)} requests for user ID {user_id}")
for req in requests_data:
media_type = req["media"]["mediaType"]
tmdb_id = req["media"]["tmdbId"]
title = req["media"].get("title", req["media"].get("name", "Unknown"))
logger.debug(f"Request: {media_type} '{title}' (tmdbId:{tmdb_id})")
return requests_data
except Exception as e:
logger.error(f"Failed to fetch requests for user ID {user_id}: {str(e)}")
return []
def migrateRequests(userOldID: int, userNewID: int) -> bool:
"""Migrate requests from Overseerr to Jellyseerr for a specific user.
Args:
userOldID: Source user ID in Overseerr
userNewID: Target user ID in Jellyseerr
Returns:
bool: True if all requests were migrated successfully, False if there were any failures
"""
try:
logger.info(f"Migrating requests from Overseerr user ID {userOldID} to Jellyseerr user ID {userNewID}")
# Verify the user exists in Jellyseerr
if not verify_user_exists(userNewID):
logger.error(f"User ID {userNewID} does not exist in Jellyseerr or could not be verified")
return False
# Get all requests for this user
user_requests = [r for r in SOURCE_REQUESTS if r["requestedBy"]["id"] == userOldID]
total_requests = len(user_requests)
logger.info(f"Found {total_requests} requests for user ID {userOldID}")
if not user_requests:
return True # No requests to migrate is considered success
# Pre-filter requests that already exist
new_requests = []
existing_count = 0
for request in user_requests:
if is_request_exists(request, TARGET_REQUESTS):
existing_count += 1
logger.info(f"Request for {request['media']['mediaType']} (tmdbId:{request['media']['tmdbId']}) already exists in Jellyseerr, skipping")
else:
new_requests.append(request)
logger.info(f"Found {existing_count} existing requests, {len(new_requests)} new requests to migrate")
if not new_requests:
logger.info("All requests already exist in Jellyseerr, skipping migration")
return True
# Migrate only new requests
success_count = existing_count # Start with existing requests as successes
failure_count = 0
for request in new_requests:
try:
tmdb_id = request["media"]["tmdbId"]
media_type = request["media"]["mediaType"]
# Fetch media details from TMDB
tmdb_details = fetch_tmdb_media_details(tmdb_id, media_type)
media_name = tmdb_details.get('title', tmdb_details.get('name', 'Unknown'))
# Log user ID information before creating payload
logger.info(f"Creating request for {media_type} '{media_name}' (tmdbId:{tmdb_id}) with user ID {userNewID} (type: {type(userNewID).__name__})")
payload = create_request_payload(request, userNewID)
# Log the payload for debugging
logger.debug(f"Sending request payload: {json.dumps(payload)}")
r = requests.post(
url=f"{TARGET_URL}/request",
headers={"X-Api-Key": TARGET_APIKEY},
json=payload,
timeout=30
)
# Capture and log the full response, even if it's an error
try:
r.raise_for_status()
# Check the response content
response_data = r.json()
if 'id' in response_data:
request_id = response_data['id']
logger.info(f"Added request for {media_type} '{media_name}' (tmdbId:{tmdb_id}) to Jellyseerr - Request ID: {request_id}")
# Verify the request was created by fetching it back
if verify_request_created(request_id):
logger.info(f"Verified request ID {request_id} exists in Jellyseerr")
success_count += 1
else:
logger.warning(f"Could not verify request ID {request_id} exists in Jellyseerr")
failure_count += 1
else:
logger.warning(f"Request for {media_type} '{media_name}' (tmdbId:{tmdb_id}) may not have been created properly. Response: {json.dumps(response_data)}")
failure_count += 1
except requests.exceptions.HTTPError as e:
# For TV shows, include specific seasons in the error log
seasons_str = ""
if media_type == 'tv':
seasons = [season["seasonNumber"] for season in request.get("seasons", [])]
seasons_str = f", seasons:{seasons}"
# Get the response content if available
response_text = ""
try:
response_text = f" Response: {r.text}"
except:
pass
logger.error(f"Failed to migrate request for {media_type} '{media_name}' (tmdbId:{tmdb_id}{seasons_str}): {str(e)}{response_text}")
failure_count += 1
except RequestException as e:
# For TV shows, include specific seasons in the error log
seasons_str = ""
if media_type == 'tv':
seasons = [season["seasonNumber"] for season in request.get("seasons", [])]
seasons_str = f", seasons:{seasons}"
# Get the response content if available
response_text = ""
if hasattr(e, 'response') and e.response:
try:
response_text = f" Response: {e.response.text}"
except:
pass
logger.error(f"Failed to migrate request for {media_type} '{media_name}' (tmdbId:{tmdb_id}{seasons_str}): {str(e)}{response_text}")
failure_count += 1
continue
except Exception as e:
# For TV shows, include specific seasons in the error log
seasons_str = ""
if media_type == 'tv':
seasons = [season["seasonNumber"] for season in request.get("seasons", [])]
seasons_str = f", seasons:{seasons}"
logger.error(f"Unexpected error processing request for {media_type} '{media_name}' (tmdbId:{tmdb_id}{seasons_str}): {str(e)}")
failure_count += 1
continue
logger.info(f"Request migration completed. Success: {success_count}/{total_requests} ({success_count/total_requests*100:.1f}%), Failures: {failure_count}/{total_requests} ({failure_count/total_requests*100:.1f}%)")
# Verify all requests were created by fetching them from Jellyseerr
logger.info(f"Verifying all requests for user ID {userNewID} in Jellyseerr")
jellyseerr_requests = fetch_user_requests(userNewID)
if jellyseerr_requests:
logger.info(f"Found {len(jellyseerr_requests)} requests for user ID {userNewID} in Jellyseerr")
else:
logger.warning(f"No requests found for user ID {userNewID} in Jellyseerr")
return failure_count == 0
except Exception as e:
logger.error(f"Failed to migrate requests for user {userOldID}: {str(e)}")
return False
if __name__ == '__main__':
main()