我想:
1/获取YouTube频道或播放列表
2/获取视频的网址。
3/处理它们:查看它们给用户,或通过Anotrher下载管理器开始下载。
从youtube-dl获取信息的函数:
感谢JaimeMF的回答
import time
import youtube_dl
def myfunc(url):
ydl = youtube_dl.YoutubeDL()
# Add all the available extractors
ydl.add_default_info_extractors()
result = ydl.extract_info( url , download=False)
## ydl.extract_info() collect `url fetched info` in local variable called "ie_result"; It return this "ie_result" when finshed.
if 'entries' in result: # Can be a playlist or a list of videos
video = result['entries'][0]
else:
# Just a video
video = result # get desired data
res.append(desired data)
return res
问题:
如果url
youtube:channel
或youtube:playlist
; youtube-dl 会逐个获取网址; 花费很长时间返回信息; you can imagine the time of fetching cannel contaiing 500 videos information
.
所以我想同时获取res
列表;开始向用户查看它们或开始由另一个下载器下载它们。
试验:
我认为在以下设计中:
def get_res():
args = ['http://www.youtube.com/channel/url']
thread1 = threading.Thread(target=myfun , args )
thread1.run()
# access myfun.res.ie_result by anyway #.... this is the problem
# return its value
def worker():
processed_res = []
while True:
res = get_res()
for item in res :
if item not in processed_res:
# do something like;
# start viewing it to the user,
# or start downloading them by another downloader.
processed_res.append(item)
time.sleep(1)
# break when tread1 terminate
get_res()
worker()
代码缺陷:
实际问题仍然存在于函数get_res
中的*访问myfun.res.ie_result *;
简述:
是否可以从其他线程访问运行函数的局部变量?任何帮助都值得赞赏:D任何通过另一种方式解决问题的试验也是。
* 要记住的项目:*
我已经编辑了代码以澄清它。
myfun
在其result
局部变量中调用extract_info()
函数。
extract_info()
函数在名为ie_result
的变量中收集结果extract_info
逐个获取 url 数据,并在列表中返回所有内容。 收集它。
myfun.res.ie_result
等于ydl.extract_info(url).ie_result
你的问题有点模糊,需要上下文中的代码来回答它。但我认为课程应该是解决方案您需要创建一个类并在其中列出您的函数定义。然后,您应该将前缀"self."添加到要全局访问的任何局部变量中。
对于如何创建类:https://docs.python.org/2/tutorial/classes.html