Win Reuse variable created in main folder in subfolder
#1
I'm trying to reuse a variable I created in the main folder when accessing one of the subfolders. Is this possible? And how would I go about it?
The variable 'pages' does an API call, and I need it to build the main menu. However, I want to limit the number of API calls I make.
So in the submenu, I would like to reuse the information in the 'pages' variable, instead of making the same API call (because the API call is slow, and of course rate limiting might be an issue). Is there a way to retain a variable? Or should I use a different approach? Can I create the entire menu (all main folders and subfolders) all at once in the main menu (I'm pretty sure I can't but I want to be sure)? I'm still new to addon development, so there is a fair chance I'm doing it completely wrong. Please be patient with me. 

python:
if mode is None:
    pages = get_videos()
    items = unique_items(pages)
    for item in items:
        url = build_url({'mode':'item', 'foldername':item.replace('_',' ')})
        li = xbmcgui.ListItem(item.replace('_',' '))
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url, 
            listitem=li, isFolder=True)
    xbmcplugin.endOfDirectory(addon_handle)

elif mode[0] == 'item':
    events = get_items(pages, mode[0])
    years = unique_years(events)
    for year in years:
        url = build_url({'mode':year, 'foldername':year})
        li = xbmcgui.ListItem(year)
        xbmcplugin.addDirectoryItem(handle=addon_handle, url=url,
            listitem=li, isFolder=True)
    xbmcplugin.endOfDirectory(addon_handle)

PS sorry for the formatting, I have no idea why the spacing is so huge in the code here on the forum
Reply
#2
If your pages variable is of a simple type (a string or a number) you can pass it via an URL query string. URL string syntax even allow to pass lists of strings, provided they are short ones. But in a general case I'd recommend to serialize your pages variable as JSON or Python pickle and save it to disk. You may also add a timestamp to implement some expiration mechanism for your data.

Addon's profile directory is the recommended place where your addon can store arbitrary data:
python:
profile_dir = Addon().getAddonInfo('profile').decode('utf-8')

P.S. There's nothing wrong with your spacing as far as I can see. 4-spaces indent is the standard in Python.
Reply
#3
(2017-12-07, 09:54)Roman_V_M Wrote: If your pages variable is of a simple type (a string or a number) you can pass it via an URL query string. URL string syntax even allow to pass lists of strings, provided they are short ones. But in a general case I'd recommend to serialize your pages variable as JSON or Python pickle and save it to disk. You may also add a timestamp to implement some expiration mechanism for your data.

Addon's profile directory is the recommended place where your addon can store arbitrary data:
python:
profile_dir = Addon().getAddonInfo('profile').decode('utf-8')

P.S. There's nothing wrong with your spacing as far as I can see. 4-spaces indent is the standard in Python.
Adding a timestamp was already what I was thinking about to minimize the amount of API requests even further. Writing it to disk would be the best option, because the data structure is a mess (thanks Vimeo API). Thanks for providing the data storage path, I wouldn't have thought of it and it was one of my main concerns. I'll look into serializing the data to JSON and writing it to the path. Thanks for all the help. I'll try to get as far as I possibly can, this should be doable for me. I suppose I'll ask again if I get hopelessly stuck again, but I don't want to bother people on here too much.

Also; yeah the code seems to be fine again, it used to display with an enter between every line which was odd. Now it's back to being fine again.
Reply

Logout Mark Read Team Forum Stats Members Help
Reuse variable created in main folder in subfolder0