Get unread Reddit posts, create notifications and read them on Chrome Custom Tabs 2016-09-23

Learn how you can read your favorite Reddits on a Chrome Custom Tab via notifications

  1. joaomgcd
    This is what the finished project will look like:



    In this project you have 2 Tasks and 2 Profiles:
    2 Tasks
    • Get New Reddit Posts: This is the main task which takes a subreddit as a parameter
    • Test Reddit Posts: This is just a test task that calls the main task with a certain subreddit. In this case it's testing it with the Tasker subreddit but you can easily change it
    2 Profiles
    • Check Reddit Post: will open a reddit post in a Chrome Custom Tab
    • Clear Notification: will clear a notification with a certain ID
    You also get 2 icons in the Zip:
    • redditicon.png: is used as the icon on posts that don't have their own icon. You can change the path in Action 1 in the Get New Reddit Posts task.
    • redditstatusbar.png: is used as the status bar icon. Only usable on Android 6 and above. You can change the path in Action 2 in the Get New Reddit Posts task.
    Explanations:

    Get New Reddit Posts Task

    Code (Text):

    Get New Reddit Posts (17)
        A1: Variable Set [ Name:%bigicon To:/storage/emulated/0/Join/Files/redditicon.png Do Maths:Off Append:Off ]
        A2: Variable Set [ Name:%smallicon To:/storage/emulated/0/Join/Files/redditstatusbar.png Do Maths:Off Append:Off ]
     
    Setting icons in variables so their path is easier to change
    Code (Text):

        A3: Variable Set [ Name:%subreddit To:%par1 Do Maths:Off Append:Off ] If [ %par1 Set ]
        A4: Variable Set [ Name:%subreddit To:funny Do Maths:Off Append:Off ] If [ %subreddit !Set ]
     
    Setting default subreddit in case you want to test this task right here without having to call it from another task
    Code (Text):

        A5: AutoTools Json Read [ Configuration:Json: https://www.reddit.com/r/%subreddit/.json
    Fields: data.children.data.created_utc(),data.children.data.title(),data.children.data.permalink(),data.children.data.thumbnail()
    Query: permalink
    Sort Array Invert: false
    Get Parent Values: false
    Variable Name: postid(),title(),link(),icon()
    Separator: , Timeout (Seconds):60 ]
        A6: [X] Flash [ Text:%title(#)
    %postid(#)
    %link(#)
    %icon(#) Long:Off ]
     
    Reading data from a subreddit. The url of the subreddit is based on the %subreddit variable that the task receives as a parameter.
    4 fields are read: created_utc, title, permalink and thumbnail. These are renamed as postid,title,link and icon for the output for easy reading.
    As you can see created_utc, which is a date, is used as the post ID since it's very unlikely that 2 posts are created at the same exact millisecond.
    Code (Text):

        A7: Variable Set [ Name:%joiner To:-+-+ Do Maths:Off Append:Off ]
        A8: Variable Join [ Name:%postid Joiner:%joiner Delete Parts:On ]
        A9: Variable Join [ Name:%title Joiner:%joiner Delete Parts:On ]
        A10: Variable Join [ Name:%link Joiner:%joiner Delete Parts:On ]
     
    Joining all arrays with -+-+ (could be anything that you think doesn't appear in post titles).
    This is done because if there are commas in post titles then in the AutoTools Arrays action ahead AutoTools would think that there are more titles then there are in reality.
    For example, if a post's title is hello, I'm allo! AutoTools would think that these are 2 titles instead of just one because there's a comma there. By making the separator be -+-+ you avoid that issue.
    Also, Delete Parts is selected so that new arrays with the same names but less values can be created later. More info here.
    Code (Text):

        A11: AutoTools Text [ Configuration:Text: %icon()
    Variable Name: icon()
    Monetize: false
    Pad Right: false
    Trim: false
    Replacements: %icon\d+=:=self
    Regex: true
    Joiner Variable: atjoinedtext
    Exact: false
    Regex: false
    Case Insensitive: false
    Contains All: false
    Url Encode: false
    Dip to Pixels: false
    Pixels To Dip: false
    RGB To HSV: false
    RGB To Phillips Hue Color: false
    Separator: , Timeout (Seconds):60 ]
     
    Some subreddits don't have icons for their posts and the %icon() array would end up like %icon1,%icon2,%icon3 etc. In this action we replace any instance of %icon followed by a number with the word self which will be replaced by the default icon later in the task. Some subreddits set their icons to self when there is not an icon for a post, so that takes care of that too.
    This makes %icon1,%icon2,%icon3 turn into self,self,self for example.
    Code (Text):

        A12: Variable Join [ Name:%icon Joiner:%joiner Delete Parts:On ]
     
    Joining the icon list after the replacements above
    Code (Text):

        A13: AutoTools Arrays [ Configuration:Input Arrays: %postid|||%title|||%link|||%icon
    Vertical Mode: false
    Separator: |||
    Item Separator: %joiner
    Input Is File: false
    Names: postid,title,link,icon
    Output Variables Separator: ,
    Sort Arrays: false
    Invert: false
    Randomize: false
    Merge Arrays: false
    Merged Array Name: atmergedarray
    Unique: false
    Exact: false
    Regex: false
    Case Insensitive: false
    Contains All: false
    Name: reddit%subreddit
    Clear For Name: false
    Clear For All: false Timeout (Seconds):60 ]
     
    Taking all the arrays and filtering them so that only new items remain, based on the items in %postid. More info on this here.
    Code (Text):

        A14: Return [ Value:0 Stop:On ] If [ %postid(#) ~ 0 ]
     
    If there are no new items return 0 to the calling task
    Code (Text):

        A15: For [ Variable:%index Items:1:%postid(#) ]
     
    Go through all the items in the array...
    Code (Text):

        A16: Variable Set [ Name:%icon To:%icon(%index) Do Maths:Off Append:Off ]
        A17: Variable Set [ Name:%icon To:%bigicon Do Maths:Off Append:Off ] If [ %icon ~ self ]
     
    ...if a notification icon is self replace it with the default icon...
    Code (Text):

        A18: AutoNotification [ Configuration:Use HTML: false
    Title: New %subreddit Post on Reddit
    Text: %title(%index)
    Action on Touch: checkredditpost=:=%postid(%index)=:=%link(%index)
    Icon: %icon
    Status Bar Icon Manual: %smallicon
    Status Bar Text Size: 16
    Id: %postid(%index)
    Is Group Summary: false
    Skip Picture Cache: false
    Update Notification: false
    Only on Phone: false Timeout (Seconds):20 ]
     
    ...and create a notification with the a title, text set to the post's title, icon set to the big icon, status bar set to the small icon and the Action On Touch set to checkredditpost=:=%postid(%index)=:=%link(%index).
    This will make clicking the notification send a command like
    checkredditpost=:=1474636339=:=/r/pics/comments/544mh0/above_and_below/
    for example.
    This will trigger the Check Reddit Post profile and open the Chrome Custom Tab.
    Code (Text):

        A19: End For
        A20: Return [ Value:%postid(#) Stop:On ]
     
    At the end return the number of new posts to the calling task.



    Check Reddit Post Profile

    Code (Text):

    Profile: Check Reddit Post (18)
        Event: AutoApps Command [ Configuration:Command Filter: checkredditpost=:=
    Case Insensitive: false
    Exact: false
    Regex: false
    Variable Names: postid,link
    Variable Array: false ]
     
    This condition will trigger if the sent command contains checkredditpost=:=. This is the type of command that is sent when the notification is clicked.
    Also, 2 variables are set: postid and link. postid will contain the value after the first =:= (the post's Id) and link will contain the value after the second =:= (the post's link)
    Code (Text):

    Enter: Anon (19)
        A1: Flash [ Text:Opening reddit Post... Long:Off ]
        A2: AutoTools Chrome Custom Tabs [ Configuration:Url: https://www.reddit.com/%link
    Toolbar Color: #FF64B5F6
    Action Button Command: clearnotification=:=%postid
    Action Button Image: android.resource://net.dinglisch.android.taskerm/hd_navigation_accept
    Enable Url Bar Hiding: true
    Continue Task: true Timeout (Seconds):300 ]
     
    This action will open a new Chrome Custom Tab where the URL is the link that was sent with the command.
    It'll have a button that will dismiss the notification related to this post.
    This is done with the clearnotification=:=%postid command, which will trigger the Clear Notification profile which clears a notification with the given ID.

Recent Reviews

  1. CSWinnall
    CSWinnall
    5/5,
    Version: 2016-09-23
    Great I use it everyday