def grabChannelMetadata(self,channel):
'''
Takes a channel in the form of /user/username or /channel/hashvalue
as a string. Downloads the webpage of the channel into the cache.
Then reads the channels metadata into the channelCache for later
use.
'''
# if the channel timer has expired or the channel does not
# yet exist in the cache we need to update the channel data
if self.checkTimer(channel+':meta','channelMetadataDelay') or\
channel not in self.channelCache.names:
# if channel is not in the cache then grab info from the website
##############
# user channel information can be found by downloading the
# user channel page with
#"https://youtube.com"userName
channelPage=self.cacheWebpage("https://www.youtube.com"+channel)
# jerk out the banner image from the downloaded user webpage
try:
temp=channelPage.split('.hd-banner-image {background-image: url(//')
temp=temp[1]
temp=temp.split(');')
# append https to the picture so it will work
fanArt="https://"+temp[0]
except:
# if this does not work set the fanart to none
fanArt='none'
# split the page based on tag opening
channelPage=channelPage.split("<")
for tag in channelPage:
# the channels metadata is stored in a image tag for the users
# profile picture, so search for
#'class="channel-header-profile-image"'
if 'class="channel-header-profile-image"' in tag:
# inside this string you will have two important variables
# - first src="" will have the icon you should use for the channel
# - second title="" will have the human readable channel title
# you should store these things in the cache somehow to use them
# when rendering the channels view
# grab text in src attribute between parathenesis
icon=tag.split('src="')
icon=icon[1].split('"')
icon=icon[0]
# if a generated channel uses the other wierd icon format
if icon[:2]=='//':
icon='https:'+icon
# grab text in title attribute for channel title
title=tag.split('title="')
title=title[1].split('"')
title=title[0]
# clean html entities from title
title=self.cleanText(title)
# add channel information to the channel cache
tempChannelCache=dict()
# add title and icon
tempChannelCache['title']=title
tempChannelCache['icon']=icon
tempChannelCache['fanArt']=fanArt
self.channelCache.saveValue(channel,tempChannelCache)
评论列表
文章目录