[GH-ISSUE #79] reusing ytmusic after uploads - Unknown Error #59

Closed
opened 2026-02-27 22:07:48 +03:00 by kerem · 1 comment
Owner

Originally created by @tonyhallett on GitHub (Sep 15, 2020).
Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/79

The code below does not throw an error but does if the same instance is used.

Uploading all tracks in a folder, then getting their video ids ( using the difference from before ) and adding to existing playlist.

def getYTMusic():
  return YTMusic('headers_auth.json')

def UploadFolderTracksAndAddToPlaylistWorker(folder:str,playlistId:str,initialFetchLimit:int,sleepTime:float) -> UploadFolderTracksAndAddToPlaylistResult:
  # ytmusic.get_library_upload_songs 
  uploadVideoIdsBefore = GetLatestUploadVideoIds(getYTMusic(),initialFetchLimit)
  ytmusic = getYTMusic();
  
  notUploadable = []
  uploadResponses = []  
  numSuccessfulUploads = 0
  for root, dirs, files in os.walk(folder, topdown=False):
    for name in files:
      fullPath = os.path.join(root, name)
      if IsUploadable(name):
        response = ytmusic.upload_song(fullPath)
        if successfulResponse(response):
          uploadResponses.append(UploadResponse(fullPath,True))
          numSuccessfulUploads = numSuccessfulUploads + 1
        else:
          uploadResponses.append(UploadResponse(fullPath,False))
      else:
        notUploadable.append(fullPath)

  newFetchLimit = initialFetchLimit + len(uploadResponses)

  print("sleeping to allow for videoIds *********************")
  time.sleep(sleepTime)
  
  uploadVideoIdsAfter = GetLatestUploadVideoIds(getYTMusic(),newFetchLimit)

  newUploadVideoIds = list(filter(lambda videoId:not videoId in uploadVideoIdsBefore,uploadVideoIdsAfter))

  numNewUploadVideoIds = len(newUploadVideoIds)
  if numNewUploadVideoIds != numSuccessfulUploads:
    print("Not all videoids present") # could sleep and retrieve again

  updatePlaylistResponse = getYTMusic().add_playlist_items(playlistId,newUploadVideoIds)
  return UploadFolderTracksAndAddToPlaylistResult(notUploadable,uploadResponses,newFetchLimit,updatePlaylistResponse)
  

response object

{'error': {'code': 400, 'errors': [...], 'message': 'Unknown error.', 'status': 'INVALID_ARGUMENT'}} 
special variables: 
function variables: 'error': {'code': 400, 'errors': [{...}], 'message': 'Unknown error.', 'status': 'INVALID_ARGUMENT'} 
special variables: 
function variables: 
'code': 400 
'message': 'Unknown error.' 
'errors': [{'domain': 'gdata.CoreErrorDomain', 'location': 'client', 'locationType': 'other', 'message': 'Unknown error.', 'reason': 'INVALID_VALUE'}] 
special variables: 
function variables: 
0: {'domain': 'gdata.CoreErrorDomain', 'location': 'client', 'locationType': 'other', 'message': 'Unknown error.', 'reason': 'INVALID_VALUE'} 
len(): 1 
'status': 'INVALID_ARGUMENT' 
len(): 4 
len(): 1
Originally created by @tonyhallett on GitHub (Sep 15, 2020). Original GitHub issue: https://github.com/sigma67/ytmusicapi/issues/79 The code below does not throw an error but does if the same instance is used. Uploading all tracks in a folder, then getting their video ids ( using the difference from before ) and adding to existing playlist. ``` def getYTMusic(): return YTMusic('headers_auth.json') def UploadFolderTracksAndAddToPlaylistWorker(folder:str,playlistId:str,initialFetchLimit:int,sleepTime:float) -> UploadFolderTracksAndAddToPlaylistResult: # ytmusic.get_library_upload_songs uploadVideoIdsBefore = GetLatestUploadVideoIds(getYTMusic(),initialFetchLimit) ytmusic = getYTMusic(); notUploadable = [] uploadResponses = [] numSuccessfulUploads = 0 for root, dirs, files in os.walk(folder, topdown=False): for name in files: fullPath = os.path.join(root, name) if IsUploadable(name): response = ytmusic.upload_song(fullPath) if successfulResponse(response): uploadResponses.append(UploadResponse(fullPath,True)) numSuccessfulUploads = numSuccessfulUploads + 1 else: uploadResponses.append(UploadResponse(fullPath,False)) else: notUploadable.append(fullPath) newFetchLimit = initialFetchLimit + len(uploadResponses) print("sleeping to allow for videoIds *********************") time.sleep(sleepTime) uploadVideoIdsAfter = GetLatestUploadVideoIds(getYTMusic(),newFetchLimit) newUploadVideoIds = list(filter(lambda videoId:not videoId in uploadVideoIdsBefore,uploadVideoIdsAfter)) numNewUploadVideoIds = len(newUploadVideoIds) if numNewUploadVideoIds != numSuccessfulUploads: print("Not all videoids present") # could sleep and retrieve again updatePlaylistResponse = getYTMusic().add_playlist_items(playlistId,newUploadVideoIds) return UploadFolderTracksAndAddToPlaylistResult(notUploadable,uploadResponses,newFetchLimit,updatePlaylistResponse) ``` response object ``` {'error': {'code': 400, 'errors': [...], 'message': 'Unknown error.', 'status': 'INVALID_ARGUMENT'}} special variables: function variables: 'error': {'code': 400, 'errors': [{...}], 'message': 'Unknown error.', 'status': 'INVALID_ARGUMENT'} special variables: function variables: 'code': 400 'message': 'Unknown error.' 'errors': [{'domain': 'gdata.CoreErrorDomain', 'location': 'client', 'locationType': 'other', 'message': 'Unknown error.', 'reason': 'INVALID_VALUE'}] special variables: function variables: 0: {'domain': 'gdata.CoreErrorDomain', 'location': 'client', 'locationType': 'other', 'message': 'Unknown error.', 'reason': 'INVALID_VALUE'} len(): 1 'status': 'INVALID_ARGUMENT' len(): 4 len(): 1 ```
kerem 2026-02-27 22:07:48 +03:00
  • closed this issue
  • added the
    bug
    label
Author
Owner

@sigma67 commented on GitHub (Sep 15, 2020):

Ah yes, I believe this error is caused by assigning a reference to self.headers and then modifying it, instead of making a shallow copy. Good catch.

Can you change
github.com/sigma67/ytmusicapi@9bded79ea2/ytmusicapi/mixins/uploads.py (L223)

in your local install to

headers = self.headers.copy()

and report back?

I believe that should fix it.

<!-- gh-comment-id:692759215 --> @sigma67 commented on GitHub (Sep 15, 2020): Ah yes, I believe this error is caused by assigning a reference to self.headers and then modifying it, instead of making a shallow copy. Good catch. Can you change https://github.com/sigma67/ytmusicapi/blob/9bded79ea22627fc35d2a9f235fb4d76e8948e68/ytmusicapi/mixins/uploads.py#L223 in your local install to ```python headers = self.headers.copy() ``` and report back? I believe that should fix it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ytmusicapi#59
No description provided.