2015-05-08 309 views
0

我必須檢索包含某些YouTube視頻鏈接的推文。然而,Twitter使用t.co網址縮略服務的服務短網址,因此,如果用戶的鳴叫以下鏈接https://www.youtube.com/watch?v=x6QZn9xiuOE,鳴叫讀取像https://t.co/bYeHhy9kAU以t.co的方式縮短網址

因此,我不能使用原來的網址,但我必須跑使用縮短的網址的查詢。

如何從原始網址開始獲取t.co縮短的網址?

+0

是什麼請求http://t.co/HdshdsHe回來嗎?服務器設置了哪些標頭? –

+0

這是以上YouTube視頻的縮短網址:https://t.co/bYeHhy9kAU – stochazesthai

+0

t.co是否有公開API?如果沒有,你運氣不好。 –

回答

1

我通過在我的時間軸上發送原始URL,然後閱讀其縮短版本,最後使用縮短的URL運行查詢來解決了我的問題。

這裏是Python代碼我使用:

twitter_api = twitter.Twitter(auth = auth) 

# searching for tweets 
video_list = ["HdwMY2Fa7G4","FuqLui0_EF8","ZLUSg1_-o4c", 
"jvN5OwkwwmE","D2H839E2PIw","94wmdh23JsQ", 
"MIXuXnnW4io","9Jo0uk9ewWQ","9eBHdFpmpHs", 
"czr4nUEF77s","Q-P9ygH6T20","SWnXj8Nkpic", 
"5IgvJ7mEl4c","grZPvHb0yA8","Gwk6FmiAKCo", 
"vCub9qk4vTk","PX0qDfYNykc","HLz_4NVSO6c", 
"rTB5kwmv9D4","gXwZ3dbIhjw"] 

k = 0 # counter 
for vid in video_list: 
    k += 1 
    # settings 
    count = 10 
    video_url = 'https://www.youtube.com/watch?v='+vid 

    # tweet video URL in my timeline 
    twitter_api.statuses.update(status = video_url) 

    # retrieve shortened URL from my last tweet 
    my_account = 'ibbessi' 
    args = {'count' : 1} 
    timeline = twitter_api.statuses.user_timeline(screen_name = my_account, **args) 
    shortened_url = timeline[0]['text'] 
    print '# ', str(k), '\noriginal URL:', video_url, '\nshortened URL:', shortened_url, '\n' 

    # search the shortened URL 
    search_results = twitter_api.search.tweets(q = shortened_url, count = count) 
    statuses = search_results['statuses'] 

    # output shortened URL 
    print 'I have found ' + str(len(statuses)) + ' tweet(s) with video ' + shortened_url + '\n' 
    for i in range(0,len(statuses)): 
     print '#', str(i+1), '\nTEXT: ' , statuses[i]['text'], '\nUSER: ', statuses[i]['user']['name'], '\nRT COUNT: ', statuses[i]['retweet_count'], '\nDATE: ', statuses[i]['created_at'],'\n' 
+0

這實際上會工作嗎?難道他們不會重新延長每條新推文的每個網址嗎? – cnst