Set Muzei Wallpapers from Subreddit feed

Learn how to read a subreddit, get all the image links and set them as wallpapers with Muzei

  1. joaomgcd
    By following this example you'll be able to get any subreddit's front page image links and use them as wallpapers with Muzei.

    In this example we're going to be getting wallpapers from https://www.reddit.com/r/ImaginaryCastles/

    Go directly to the last step to see how it looks in the end.

    STEP 1 - CREATE TASK AND READ JSON FROM URL


    /!\You need to add /.json at the end of a reddit URL so that the data returned is in the JSON format and not a normal HTML page.

    (i) Simple mode is used when reading Json objects in a very easy but more limited way.

    In this example we want to read the urls, titles and comment links from the reddit feed.

    This is a very simple version of the reddit feed to illustrate the example:
    Code (Text):

    {
        "data": {
            "children": [
                {
                    "kind": "t3",
                    "data": {
                        "permalink": "/r/ImaginaryCastles/comments/51a8kr/messenger_of_iron_islands_by_uros_sljivic/",
                        "url": "https://cdna0.artstation.com/p/assets/images/images/001/995/456/large/uros-sljivic-messanger-of-iron-islands.jpg?1455703291",
                        "title": "Messenger Of Iron Islands by Uros Sljivic",
                        "preview": {
                            "images": [
                                {
                                    "source": {
                                        "url": "https://i.redditmedia.com/yYrKSEWLjGIlHKrFO79f8287Nz9_JoRxYXmbjta0klw.jpg?s=2052c17e6cafb2496cc6b553fbe2efa7"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        }
    }
     
    With Simple Mode we could simply set the fields to url,title,permalink and it would get the values. But since there are other fields in the reddit feed with that are named url (in the example above you can see that there is an images array with a source.url field in it) it would not work correctly because AutoTools would also get those fields and it would break the results.

    So, because of that, you have to use the "Non-Simple" version to read fields with their full path as seen below so that you get exactly the fields you want.


    STEP 2 - SET FIELDS


    • In the Fields field write data.children.data.url(),data.children.data.title(),data.children.data.permalink()
    (i) Each field path has the () suffix so that the fields are returned as arrays. If the prefix hadn't been used, only the first value for each field would be returned.
    Check the Json example above and check out how the path of the field names specified here corresponds to the path in the Json structure.


    STEP 3 - ONLY GET IMAGES


    • Tap Filter at the bottom
    • Set the Filter Fields to data.children.data.url
    (i) This will make AutoTools filter results based on the url field
    • Set the Filter Value field to (\.jpg)|(\.png) and enable the Regex option
    (i) This is a regex that matches any text that has .jpg or .png in it. This will effectively filter out anything that isn't an image with the jpg or png extension
    • Go back to Tasker and add a Flash action
    • Write %data_children_data_url(#),%data_children_data_url(1),%data_children_data_title(1),%data_children_data_permalink(1)
    (i) This will make the flash show how many elements the Json Read action returned as well as the first url, title and permalink.
    Notice how the permalink starts with /r/ and not with https://www.reddit.com. We want to get working links so we need to prefix all links with https://www.reddit.com to make them work correctly. We'll do that in the next step.


    STEP 4 - ADD URL PREFIX


    • Add an AutoTools Text action
    • Set the Text field to %data_children_data_permalink()
    • Set the Variable Name field to data_children_data_permalink()
    (i) Notice how we used % in the Text field but not in the Variable Name field. That was because we want to use the contents of the %data_children_data_permalink() variable in the Text field, but we do NOT want to use the contents in the Variable Name field. We simply want to name the output variable with that name
    • Set the Prefix field to https://www.reddit.com
    • Go back to Tasker and move the Flash action to the end of the task
    (i) As you can see the permalink value now has the right url prefix.


    STEP 5 - SET MUZEI WALLPAPERS


    • Add an AutoTools Muzei action
    /!\ The AutoTools Muzei options have moved into the AutoTools Image action, so add that action instead and use the Muzei subscreen there.
    • Set the Images field to %data_children_data_url()
    • Set the Titles field to %data_children_data_title()
    • Set the View URLs field to %data_children_data_permalink()
    • Enable the Next Image option
    (i) If you don't enable Next Image the wallpaper won't be set right away but will only change on the next scheduled change.
    • Set the Schedule field to 3600
    (i) This will make the wallpaper change each 3600 seconds, which is an hour


    STEP 6 - TEST


    • Run the Task
    • Go back to your home screen and notice how the wallpaper changes
    • Open Muzei and change the wallpaper by tapping the forward arrow button
    • Tap on the wallpaper title to go to the reddit comments page for the wallpaper



    That's it! You can now run task once a day for example, and it'll get the latest wallpapers and set them as your Muzei wallpapers. :cool:

    The task will get the image URLs, set them in Muzei (it will not download the files, but will use the URLs in Muzei directly. Muzei downloads the files when it needs them), and schedule automatic updates every hour. You only need to run the Task once when you want to get new images but the images will self-update in Muzei every hour automatically without any Tasker intervention.