Python Youtube API搜索超出范围

时间:2013-10-17 20:05:32

标签: python youtube youtube-api

我需要帮助。我马上就会告诉你代码。但首先,我将概述我要做的事情。这是一个程序,可以帮助我搜索Youtube最近的歌曲。

基本上,它需要一个文本文件并使用该数据在Youtube中搜索文本文件中的每个项目。我使用了readlines,然后通过制表符拆分它们。我正在使用的文本文件采用以下格式:

Track_Number [tab] Song_Title [tab] Artist [tab] Label - 进一步查看全文文件


文本文件示例Top 100 1 [tab] Lanterns [tab] Birds Of Tokyo [tab] CAP / EMI

2 [tab]当我是你的男人[tab] Bruno Mars [tab] ATL / WMA

3 [tab] Ho Hey The Lumineers [tab] INERTIA

4 [tab]在你的美丽[tab]下面Labrinth ft.Emeli Sande [tab] SME

5 [tab] Just Give Me A Reason [tab] Pink ft.Nate Ruess [tab] SME


这是代码

import gdata.youtube
import gdata.youtube.service
import webbrowser 
topSongVersion = "0.1"
yt_service = gdata.youtube.service.YouTubeService()


songListRaw = [] 
songList = []
videoData = [] 


def SearchAndBuild(keywords):
  videoData = []
  query = gdata.youtube.service.YouTubeVideoQuery()

  for songEntry in range(0, 25):
    query.orderby = 'viewCount'
    query.racy = 'include'    
    query.vq = keywords[songEntry][0] + ' ' + keywords[songEntry][-1]    
    feed = yt_service.YouTubeQuery(query)

    #videoData.append(feed.entry[songEntry].media.title.text) 
    #videoData.append(feed.entry[songEntry].GetSwfUrl())
    videoData.append(query.vq + ' ' + feed.entry[songEntry].GetSwfUrl())
    print songEntry, query.vq, feed.entry[songEntry].GetSwfUrl() 
  print(videoData[24])

 #print videoData

def SongsFromFile():
  f = open('top100.txt')
  songData = f.readlines() 
  f.close() 
  for songRaw in range(1,101):
    songListRaw.append(songData[songRaw].split('\t'))  
  for songEntry in range(0,100):
    songList.append(songListRaw[songEntry][1:-1])

  SearchAndBuild(songList)

#  print(songList[0]) 
#  print(songList[1])
#  print(testSearch[0])
#  print(testSearch[1])

SongsFromFile() 

对所有打印感到抱歉。那些是出于测试目的。如果你检查函数SearchAndBuild,你会看到我遇到问题的地方。我正在尝试使用由SongsFromFile设置的songList,然后使用该数据创建另一个包含歌曲标题和歌曲URL的列表列表。起初我尝试了“范围(0,99)中的songEntry”,但这使得索引超出了范围。我怀疑这是Youtube如何设置他们的api,所以我将其改为“songEntry in range(0,25)”

这似乎已经修复了超出范围的异常。这是Youtube api还是我错过了什么。我只是好奇为什么它迫使我分解我的查询。我是否必须再设置三个for循环才能构建所有100个项目?有没有更好的办法?

显然,这是一项正在进行中的工作。我可能做了很多愚蠢的事情并且错过了简单的解决方案。为什么我使用范围,为什么我使用列表而不是字典?如果你看到我在这里做的很荒谬,请让我知道并解释更好的方法。

此致 约什

更新10/17/13 10:00 PM

好的,所以我似乎已经修好了。我最初是对的。是。有更好的方法来做到这一点。然而,问题是Youtube的API,就像我最初怀疑的那样。查询的最大结果变量默认设置为25。我试图查询100.这就是我超出范围的原因。咨询Youtube API应该是我的第一个想法。这是一个很容易解决的问题;但是,不要指望它现在做得多。这是一项正在进行中的工作。最后,我将使用它在Youtube上查找我正在收集的数据库中的歌曲。

像我说的那样。这个程序还没有做任何有意义的事情。我有更多的代码要添加。在任何情况下,我都应该记录下来,以防其他人遇到与Google Python Youtube API类似的搜索查询问题。请记住,如果你不断超出范围,Youtube限制quires为50,默认设置为25。你必须以某种方式解决这个问题。

非常感谢HoopDady的评论让我重回正轨。

这是修改后的代码:

import gdata.youtube
import gdata.youtube.service
import webbrowser 

#-------------------------------
topPyVersion = "0.2"
yt_service = gdata.youtube.service.YouTubeService()

songListRaw = [] 
songList = []
videoData = [] 


def Ask2Open(songs):
  for i in songs:
    print i  

def SearchAndBuild(keywords):
  videoData = []
  query = gdata.youtube.service.YouTubeVideoQuery()
  for songEntry in xrange(10):
    query.orderby = 'viewCount'
    query.racy = 'include' 
    query.max_results = 10
    query.vq = keywords[songEntry][0] + ' ' + keywords[songEntry][-1] 
    feed = yt_service.YouTubeQuery(query)
    videoData.append(feed.entry[songEntry].media.title.text) 
    videoData.append(feed.entry[songEntry].GetSwfUrl())
    videoData.append(query.vq + ' ' + feed.entry[songEntry].GetSwfUrl())
    #print songEntry,'\n', query.vq,'\n', feed.entry[songEntry].GetSwfUrl() 
  Ask2Open(videoData)


def SongsFromFile():
  f = open('top100.txt')
  songData = f.readlines() 
  f.close() 
  for songRaw in range(1,101):
    songListRaw.append(songData[songRaw].split('\t'))  
  for songEntry in range(0,100):
    songList.append(songListRaw[songEntry][1:-1])

  SearchAndBuild(songList)

SongsFromFile()   

__ _ __ _ __ _ __ _ __ _ __ _ __ _ _

Text  File      ( top100.txt  )
---------------------------------
Top 100 
1   Lanterns    Birds Of Tokyo  CAP/EMI
2   When I Was Your Man Bruno Mars  ATL/WMA
3   Ho Hey  The Lumineers   INERTIA
4   Beneath Your Beautiful  Labrinth ft. Emeli Sande    SME
5   Just Give Me A Reason   Pink ft. Nate Ruess SME
6   Stay    Rihanna ft. Mikky Ekko  IDJ/UMA
7   Impossible  James Arthur    SME
8   Troublemaker    Olly Murs ft. Flo Rida  SME
9   Locked Out Of Heaven    Bruno Mars  ATL/WMA
10  Everybody Talks Neon Trees  MERCUK/UMA
11  Love Is Easy    McFly   MERCUK/UMA
12  Little Talks    Of Monsters And Men UNI/UMA
13  I Love It   Icona Pop   NEON/WMA
14  I Knew You Were Trouble Taylor Swift    BIG/UMA
15  Can’t Hold Us   Macklemore & Ryan Lewis ft. Ray Dalton  MACKLEMORE LLC
16  I Could Be The One  Avicii ft. Nicky Romero UNI/UMA
17  Thrift Shop Macklemore & Ryan Lewis ft. Wanz    MACKLEMORE LLC
18  Thank You   MKTO    SME
19  Mirrors Justin Timberlake   SME
20  Scream & Shout  will.i.am ft. Britney Spears    IGA/UMA
21  Feel This Moment    Pitbull ft. Christina Aguilera  SME
22  I Need Your Love    Calvin Harris ft. Ellie Goulding    SME
23  Try Pink    SME
24  Small Bump  Ed Sheeran  ATL/WMA
25  Same Love   Macklemore & Ryan Lewis ft. Mary Lambert    MACKLEMORE LLC
26  I Will Wait Mumford & Sons  DEW/UMA
27  Let Her Go  Passenger   INERTIA
28  What You’ve Done To Me  Samantha Jade   SME
29  Holdin On   Flume   FC/WMA
30  Hello   Stafford Brothers ft. Lil Wayne & Christina Milian  UNI/UMA
31  Sweet Nothing   Calvin Harris ft. Florence Welch    SME
32  Pompeii Bastille    VIR/EMI
33  Give Me Love    Ed Sheeran  ATL/WMA
34  Diamonds    Rihanna IDJ/UMA
35  Blow Me (One Last Kiss) Pink    SME
36  Army Of Two Olly Murs   SME
37  Still Into You  Paramore    ATL/WMA
38  Daylight    Maroon 5    A&M/UMA
39  One More Night  Maroon 5    A&M/UMA
40  Don’t You Worry Child   Swedish House Mafia CAP/EMI
41  Mountain Sound  Of Monsters And Men UNI/UMA
42  We Are Never Ever Getting Back Together Taylor Swift    BIG/UMA
43  Guns & Roses    T.I. ft. Pink   ATL/WMA
44  Hall Of Fame    The Script ft. will.i.am    SME
45  Get Lucky   Daft Punk ft. Nile Rogers, Pharrell SME
46  Drinking From The Bottle    Calvin Harris ft. Tinie Tempah  SME
47  Hey Porsche Nelly   UNI/UMA
48  Beauty & A Beat Justin Bieber ft. Nicki Minaj   IDJ/UMA
49  Blurred Lines   Robin Thicke ft. T.I & Pharrell IGA/UMA
50  Change Your Life    Little Mix  SME
51  Get Up (Rattle) Bingo Players ft. Far East Movement ONELOVE
52  It’s Time   Imagine Dragons IGA/UMA
53  The Other Side  Jason Derulo    WB/WMA
54  Waiting All Night   Rudimental ft. Ella Eyre    WMUK/WMA
55  Good Time   Owl City & Carly Rae Jepsen UNI/UMA
56  Get Along   Guy Sebastian   SME
57  Next To Me  Emeli Sande VIR/EMI
58  Suit & Tie  Justin Timberlake ft. Jay Z SME
59  ThatPOWER   Will.I.Am ft. Justin Bieber IGA/UMA
60  Not Giving In   Rudimental ft. John Newman & Alex Clare WMUK/WMA
61  Right Now   Rihanna ft. David Guetta    IDJ/UMA
62  Treasure    Bruno Mars  ATL/WMA
63  Some Nights Fun.    ATL/WMA
64  Radioactive Rita Ora    SME
65  22  Taylor Swift    BIG/UMA
66  Put Your Hands Up   Matchbox Twenty ATL/WMA
67  Best Night  Justice Crew    SME
68  Rest Of My Life Ludacris ft. Usher & David Guetta   IDJ/UMA
69  Battle Scars    Guy Sebastian ft. Lupe Fiasco   SME
70  Let Me Down Easy    Sheppard    CHUGG/MGM
71  Feel The Love   Rudimental  WMUK/WMA
72  She’s So Mean   Matchbox Twenty ATL/WMA
73  Girl On Fire    Alicia Keys SME
74  She Wolf (Falling To Pieces)    David Guetta ft. Sia    VIR/EMI
75  It’s A Beautiful Day    Michael Buble   WB/WMA
76  Let Me Love You (Until You Learn To Love Yourself)  Ne-Yo   IDJ/UMA
77  Global Concepts Robert DeLong   LBR/UMA
78  Bed Of Clouds   Swift K.I.D. ft. Guy Sebastian  NEON/WMA
79  Alive   Empire Of The Sun   CAP/EMI
80  This Is What It Feels Like  Armin Van Buuren ft Trevor Guthrie  XXX
81  Everything Has Changed  Taylor Swift ft. Ed Sheeran BIG/UMA
82  Beautiful   Mariah Carey    IDJ/UMA
83  Language    Porter Robinson HUS/UMA
84  Here’s To Never Growing Up  Avril Lavigne   SME
85  C’mon   Ke$ha   SME
86  We Come Running Youngblood Hawke    UNI/UMA
87  Let It Roll Flo Rida    ATL/WMA
88  Spectrum    Florence & The Machine  ISLUK/UMA
89  How Ya Doin?    Little Mix  SME
90  Boom Boom   Justice Crew    SME
91  Happening   Chiddy Bang CAP/EMI
92  Need U 100% Duke Dumont ft. AME MOS/UMA
93  We’ll Be Coming Back    Calvin Harris ft. Example   SME
94  Turn Up The Love    Far East Movement ft. Cover Drive   IGA/UMA
95  Don’t Wake Me Up    Prinnie MERCAU/UMA
96  Saved In A Bottle   The Potbelleez  MOS/UMA
97  Arena   B.o.B ft. Chris Brown & T.I.    ATL/WMA
98  Dear Darling    Olly Murs   SME
99  Drunk   Ed Sheeran  ATL/WMA
100 My Songs Know What You Did In The Dark (Light Em Up)    Fall Out Boy    IDJ/UMA 

我现在正在做什么


以下是下一步。万一有人好奇。

def Ask2Open(songs):
  for i in songs:
    print i  

这里是我打算使用webbrowser打开歌曲中包含的Youtube网址的地方。显然,我不想同时打开10多个网页。所以我会使用一个循环然后在遇到某些条件时爆发,或者只是询问何时进入下一个项目等等。不过我会在以后想出来。

0 个答案:

没有答案