def load_cache(self):
try:
fd = xbmcvfs.File(PLAYLISTS_FILE, 'r')
self.playlists_cache = eval(fd.read())
fd.close()
self.playlists_loaded = True
log('Loaded %s Playlists from disk.' % len(self.playlists_cache.keys()))
except:
self.playlists_loaded = False
self.playlists_cache = {}
return self.playlists_loaded
python类File()的实例源码
def save_cache(self):
try:
if self.playlists_loaded:
fd = xbmcvfs.File(PLAYLISTS_FILE, 'w')
fd.write(repr(self.playlists_cache))
fd.close()
log('Saved %s Playlists to disk.' % len(self.playlists_cache.keys()))
except:
return False
return True
def get(version):
try:
import xbmc,xbmcgui,xbmcaddon,xbmcvfs
f = xbmcvfs.File(xbmcaddon.Addon().getAddonInfo('changelog'))
text = f.read() ; f.close()
label = '%s - %s' % (xbmc.getLocalizedString(24054), xbmcaddon.Addon().getAddonInfo('name'))
id = 10147
xbmc.executebuiltin('ActivateWindow(%d)' % id)
xbmc.sleep(100)
win = xbmcgui.Window(id)
retry = 50
while (retry > 0):
try:
xbmc.sleep(10)
win.getControl(1).setLabel(label)
win.getControl(5).setText(text)
retry = 0
except:
retry -= 1
return '1'
except:
return '1'
def readLog(self, path):
try:
lf = xbmcvfs.File(path)
content = lf.read()
lf.close()
if content:
return True, content
else:
log('file is empty')
return False, LANGUAGE(32001)
except:
log('unable to read file')
return False, LANGUAGE(32002)
def getCommandActions():
commands = COMMANDS.copy()
for command in commands:
actions = commands[command]
actions = [ACTIONS[x] for x in actions]
commands[command] = actions
f = xbmcvfs.File('special://profile/addon_data/script.tvguide.fullscreen/commands.json','rb')
j = f.read()
f.close()
if j:
new_commands = json.loads(j)
for c in new_commands:
if c in commands:
commands[c] = new_commands[c]
return commands
def getCustomStreamUrls(success):
if success:
stream_urls = database.getCustomStreamUrls()
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/custom_stream_urls.ini'
f = xbmcvfs.File(file_name,'wb')
for (name,stream) in stream_urls:
write_str = "%s=%s\n" % (name,stream)
f.write(write_str.encode("utf8"))
f.close()
xbmcgui.Dialog().notification(ADDON.getAddonInfo('name'), 'Exported channel mappings')
else:
database.close()
def setCustomStreamUrls(success):
if success:
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/custom_stream_urls.ini'
f = xbmcvfs.File(file_name)
lines = f.read().splitlines()
stream_urls = [line.decode("utf8").split("=",1) for line in lines]
f.close()
database.setCustomStreamUrls(stream_urls)
xbmcgui.Dialog().notification(ADDON.getAddonInfo('name'), 'Imported channel mappings')
else:
database.close()
def getAltCustomStreamUrls(success):
if success:
stream_urls = database.getAltCustomStreamUrls()
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/alt_custom_stream_urls.tsv'
f = xbmcvfs.File(file_name,'wb')
for (name,title,stream) in stream_urls:
write_str = "%s\t%s\t%s\n" % (name,title,stream)
f.write(write_str.encode("utf8"))
f.close()
xbmcgui.Dialog().notification(ADDON.getAddonInfo('name'), 'Exported alternative channel mappings')
else:
database.close()
def setAltCustomStreamUrls(success):
if success:
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/alt_custom_stream_urls.tsv'
f = xbmcvfs.File(file_name)
lines = f.read().splitlines()
stream_urls = [line.decode("utf8").split("\t",2) for line in lines if '\t' in line]
f.close()
database.setAltCustomStreamUrls(stream_urls)
xbmcgui.Dialog().notification(ADDON.getAddonInfo('name'), 'Imported alternative channel mappings')
else:
database.close()
def close(self):
if not self.isClosing:
self.isClosing = True
if self.player.isPlaying():
if ADDON.getSetting('stop.on.exit') == "true":
self.player.stop()
self.clear_catchup()
f = xbmcvfs.File('special://profile/addon_data/script.tvguide.fullscreen/tvdb.pickle','wb')
try:
pickle.dump(self.tvdb_urls,f)
except:
pass
f.close()
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/custom_stream_urls_autosave.ini'
xbmcvfs.copy(file_name,file_name+".last")
f = xbmcvfs.File(file_name,'wb')
if self.database:
stream_urls = self.database.getCustomStreamUrls()
for (name,stream) in stream_urls:
write_str = "%s=%s\n" % (name,stream)
f.write(write_str.encode("utf8"))
f.close()
if self.database:
self.database.close(super(TVGuide, self).close)
else:
super(TVGuide, self).close()
def categorySearch(self):
d = xbmcgui.Dialog()
f = xbmcvfs.File('special://profile/addon_data/script.tvguide.fullscreen/category_count.ini',"rb")
category_count = [x.split("=",1) for x in f.read().splitlines()]
f.close()
categories = []
for (c,v) in category_count:
if not self.category or self.category == "All Channels":
s = "%s (%s)" % (c,v)
else:
s = c
categories.append(s)
which = d.select("Program Category Search",categories)
if which == -1:
return
category = category_count[which][0]
programList = self.database.programCategorySearch(category)
title = "%s" % category
d = ProgramListDialog(title, programList, ADDON.getSetting('listing.sort.time') == 'true')
d.doModal()
index = d.index
action = d.action
if action == ACTION_RIGHT:
self.showNext()
elif action == ACTION_LEFT:
self.showListing(programList[index].channel)
elif action == KEY_CONTEXT_MENU:
if index > -1:
self._showContextMenu(programList[index])
else:
if index > -1:
program = programList[index]
now = datetime.datetime.now()
start = program.startDate
end = program.endDate
ask = ADDON.getSetting('catchup.dialog')
if (ask == "3") or (ask=="2" and end < now) or (ask=="1" and start < now):
self.play_catchup(program)
else:
self.playOrChoose(program)
def saveActions(self):
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/actions.json'
f = xbmcvfs.File(file_name,"wb")
data = json.dumps(self.actions,indent=2)
f.write(data)
f.close()
def loadActions(self):
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/actions.json'
f = xbmcvfs.File(file_name,"rb")
data = f.read()
f.close()
if data:
self.actions = json.loads(data)
else:
self.actions = []
def exportChannelIdList(self):
channelsList = self.getChannelList(False,True)
channels = [(channel.id,channel.title) for channel in channelsList]
f = xbmcvfs.File('special://profile/addon_data/script.tvguide.fullscreen/channel_id_title.ini','wb')
for channel in sorted(channels,key=lambda x: x[1].lower()):
f.write("%s=%s\n" % (channel[0].encode("utf8"),channel[1].encode("utf8")))
f.close()
def _setCustomStreamUrl(self, channel, stream_url):
if stream_url is not None:
image = ""
if ADDON.getSetting("addon.logos") == "true":
file_name = 'special://profile/addon_data/script.tvguide.fullscreen/icons.ini'
f = xbmcvfs.File(file_name)
items = f.read().splitlines()
f.close()
for item in items:
if item.startswith('['):
pass
elif item.startswith('#'):
pass
else:
url_icon = item.rsplit('|',1)
if len(url_icon) == 2:
url = url_icon[0]
icon = url_icon[1]
if url == stream_url:
if icon and icon != "nothing":
image = icon.rstrip('/')
c = self.conn.cursor()
if image:
c.execute('UPDATE OR REPLACE channels SET logo=? WHERE id=?' , (image, channel.id))
c.execute("DELETE FROM custom_stream_url WHERE channel=?", [channel.id])
c.execute("INSERT INTO custom_stream_url(channel, stream_url) VALUES(?, ?)",
[channel.id, stream_url.decode('utf-8', 'ignore')])
self.conn.commit()
c.close()
def __init__(self, filename):
self.vfsfile = xbmcvfs.File(filename,"rb")
self.size = self.vfsfile.size()
self.bytesRead = 0
def setAddonStream(self, section, id, stream):
self.addonsParser.set(section, id, stream)
self.addonsParser.write(xbmcvfs.File(self.path,"wb"))
def getGUID():
if xbmcvfs.exists("special://temp/rrmj.key"):
f = xbmcvfs.File("special://temp/rrmj.key")
result = f.read()
f.close()
if result != "":
return result
import uuid
key = str(uuid.uuid1())
f = xbmcvfs.File("special://temp/rrmj.key", 'w')
result = f.write(key)
f.close()
return key
def save_to_file(content, filename, path=""):
if path == "":
return False
if not xbmcvfs.exists(path):
xbmcvfs.mkdirs(path)
text_file_path = os.path.join(path, filename + ".txt")
now = time.time()
text_file = xbmcvfs.File(text_file_path, "w")
json.dump(content, text_file)
text_file.close()
logger.info("saved textfile %s. Time: %f" % (text_file_path, time.time() - now))
return True
nethelper.py 文件源码
项目:plugin.video.unofficial9anime
作者: Prometheusx-git
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def __init():
'''
Contains all network related functionality.
'''
cookies = helper.get_profile() + 'cookies.txt'
net = NetHelper(cookies, False)
# Make sure the cookies exist
if not os.path.exists(cookies):
cookiesfile = xbmcvfs.File(cookies, 'w')
cookiesfile.close()
return net, cookies