2014-01-07 62 views
0

我正在使用rauth和請求來調用Beatport API。電話工程,但我很偶然地得到以下錯誤ConnectionError: HTTPSConnectionPool(host='oauth-api.beatport.com', port=443): Max retries exceeded with urlPython請求 - 連接錯誤 - 超過最大重試次數

這是追溯。

Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "scraper/songlist_top100.py", line 88, in <module> 
    'sortBy': 'releaseDate ASC'}) 
    File "C:\Python27\lib\site-packages\requests\sessions.py", line 347, in get 
    return self.request('GET', url, **kwargs) 
    File "C:\Python27\lib\site-packages\rauth\session.py", line 208, in request 
    return super(OAuth1Session, self).request(method, url, **req_kwargs) 
    File "C:\Python27\lib\site-packages\requests\sessions.py", line 335, in reques 
t 
    resp = self.send(prep, **send_kwargs) 
    File "C:\Python27\lib\site-packages\requests\sessions.py", line 438, in send 
    r = adapter.send(request, **kwargs) 
    File "C:\Python27\lib\site-packages\requests\adapters.py", line 327, in send 
    raise ConnectionError(e) 
ConnectionError: HTTPSConnectionPool(host='oauth-api.beatport.com', port=443): 
Max retries exceeded with url: /catalog/3/tracks?perPage=150& 
oauth_nonce=xxxxx&oauth_timestamp=xxxxx&facets=artistName%3A 
Avicii&oauth_signature_method=HMAC-SHA1&oauth_version=1.0& 
oauth_consumer_key=xxxxx&oauth_token=xxxxxx&sortBy=releaseDate+ASC 
&oauth_signature=xxxxx%3D&page=3 (Caused by <class 'httplib.BadStatusLine'>: '') 

這裏是我的腳本

from rauth import OAuth1Service 
import requests 
from hunt.models import DJ, Song 

def get_obj_or_none(model, **kwargs): 
    try: 
     return model.objects.get(**kwargs) 
    except model.DoesNotExist: 
     return None 

beatport_login = 'xxx' 
beatport_pass = 'xxx' 

beatport = OAuth1Service(
    name='beatport', 
    consumer_key='xxxxx', 
    consumer_secret='xxxxx', 
    request_token_url= 'https://oauth-api.beatport.com/identity/1/oauth/request-token', 
    access_token_url='https://oauth-api.beatport.com/identity/1/oauth/access-token', 
    authorize_url='https://oauth-api.beatport.com/identity/1/oauth/authorize', 
    base_url='https://oauth-api.beatport.com/json/catalog') 

request_token, request_token_secret = beatport.get_request_token(method='POST', data={ 
    'oauth_callback': 'http://www.edmhunters.com'}) 

authorize_url = beatport.get_authorize_url(request_token) 

values = { 
    'oauth_token': request_token, 
    'username': beatport_login, 
    'password': beatport_pass, 
    'submit' : 'Login', 
} 

r = requests.post('https://oauth-api.beatport.com/identity/1/oauth/authorize-submit', data=values) 

verifier = r.url.split("oauth_verifier=",1)[1] 

tokens = beatport.get_raw_access_token(request_token, request_token_secret, method='POST', data={ 
    'oauth_verifier': verifier}) 

token_string = tokens.content 

access_token = token_string[token_string.find('=')+1:token_string.find('&')] 
access_token_secret = token_string[token_string.find('t=')+2:token_string.rfind('&s')] 

session = beatport.get_session((access_token, access_token_secret)) 

for dj in DJ.objects.all(): 
    r = session.get('https://oauth-api.beatport.com/catalog/3/tracks', params={'facets': "artistName:"+dj.name, 'perPage': 150}) 
    count_response = r.json() 
    results = [] 
    for i in range(1, count_response['metadata']['totalPages']+1): 
     r1 = session.get('https://oauth-api.beatport.com/catalog/3/tracks', params={'facets': "artistName:"+dj.name, 
                        'page': i, 
                        'perPage': 150, 
                        'sortBy': 'releaseDate ASC'}) 
     json_response = r1.json() 
     results += json_response['results'] 

    song_list = []  
    for song in results: 
     artists = [artist['name'] for artist in song['artists'] if str(artist['type'])=='artist'] 
     remixers = [artist['name'] for artist in song['artists'] if str(artist['type'])=='remixer'] 
     if not ((dj.name in artists) and ((dj.name not in remixers) if len(remixers)>0 else False)): 
      song_list.append(song) 

    for song in song_list: 
     artists = [artist['name'] for artist in song['artists'] if str(artist['type'])=='artist'] 
     remixers = [artist['name'] for artist in song['artists'] if str(artist['type'])=='remixer'] 
     artist_list = ', '.join(artists) 
     remixer_list = ', '.join(remixers) 
     song_name = song['name'] 
     if not(song_name.lower().find("feat.") == -1): 
      normal_name=song_name[0:song_name.lower().find("feat.")].rstrip() 
     else: 
      normal_name=song_name 

     genre_list=[] 
     for genre in song['genres']: 
      genre_list.append(genre['name']) 
     genres = ', '.join(genre_list) 

     if not get_obj_or_none(Song, name__iexact=song_name, artist=dj): 
      s = Song(song_id=song['id'], 
        name=song_name, 
        title=song['title'], 
        normalized_name=normal_name, 
        artist=dj, 
        artists=artist_list, 
        remixers=remixer_list, 
        release_date=song['releaseDate'], 
        slug=song['slug'], 
        artwork=song['images']['large']['url'], 
        genres=genres) 
      s.save() 
      print "Added song:", s.song_id, s.artist 

爲什麼我得到上述錯誤?

+0

您的回溯看起來完全不符合您的代碼。服務器給你一個空的響應(如在,根本沒有響應)。 –

+0

@MartijnPieters我忽略了答覆部分,認爲它與錯誤無關。我也更新了響應部分的代碼。 –

+0

不知道爲什麼你會得到錯誤,但它似乎發生在第3頁,所以大概第1頁和第2頁是成功的。聽起來像是你需要用Beatport解決的問題。 –

回答

1

它看起來好像Beatport API過載並且有時會過早地關閉連接。你的第一組請求成功了,第3頁拋出錯誤,因爲響應是空的。

你真的應該報告給Beatport,但你也許可以通過指示requests模塊重試請求解決此問題:

from requests.adapters import HTTPAdapter 

# .... 

session = beatport.get_session((access_token, access_token_secret)) 
session.mount('https://oauth-api.beatport.com', HTTPAdapter(max_retries=5)) 

會重試您的要求的情況下,發生了錯誤多做幾次。

+0

但是有可能在每個'requests'重試中添加延遲? – avi

+0

@avi:不,但您可以使用第三方軟件包,如['backoff'](https://pypi.python.org/pypi/backoff)來添加延遲。 –

相關問題