Skip to content

Instantly share code, notes, and snippets.

@MorningZ
Last active October 28, 2020 00:28
Show Gist options
  • Save MorningZ/1e0ba58e371484af67687dcfb8e1e807 to your computer and use it in GitHub Desktop.
Save MorningZ/1e0ba58e371484af67687dcfb8e1e807 to your computer and use it in GitHub Desktop.
Node-RED Flow to retrieve Trakt.TV data via API
[{"id":"c53ac113.a919d","type":"tab","label":"Trakt.TV","disabled":false,"info":""},{"id":"ec79305c.42be2","type":"inject","z":"c53ac113.a919d","name":"Generate User Code","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":160,"wires":[["96bf6703.d41748"]]},{"id":"65039fcb.ef24e","type":"http request","z":"c53ac113.a919d","name":"Device Code","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":510,"y":160,"wires":[["5b11c69d.ee7ef8"]]},{"id":"70b69bf4.282ae4","type":"debug","z":"c53ac113.a919d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":915,"y":160,"wires":[],"l":false},{"id":"5b11c69d.ee7ef8","type":"json","z":"c53ac113.a919d","name":"","property":"payload","action":"obj","pretty":false,"x":615,"y":160,"wires":[["70b69bf4.282ae4","307d6238.83d7ce"]],"l":false},{"id":"307d6238.83d7ce","type":"unsafe-function","z":"c53ac113.a919d","name":"Create Message","func":"\nreturn [\n [\n {\n \"topic\": \"notification/telegram\",\n \"payload\": {\n \"text\": msg.payload.verification_url\n }\n }\n ],\n [\n {\n \"topic\": \"notification/telegram\",\n \"payload\": {\n \"text\": msg.payload.user_code,\n \"device_code\": msg.payload.device_code\n }\n }\n ]\n];","outputs":2,"noerr":0,"x":740,"y":200,"wires":[["aa951b84.488008"],["aa951b84.488008","af930e5a.7e99c"]],"outputLabels":["Link","Code"]},{"id":"aa951b84.488008","type":"mqtt out","z":"c53ac113.a919d","name":"","topic":"","qos":"0","retain":"false","broker":"333d0b5c.8ed1e4","x":915,"y":200,"wires":[],"l":false},{"id":"f0b4d2c.b7e833","type":"debug","z":"c53ac113.a919d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":975,"y":340,"wires":[],"l":false},{"id":"72cbc7af.38bbc8","type":"http request","z":"c53ac113.a919d","name":"Token Poll","method":"POST","ret":"txt","paytoqs":"ignore","url":"https://api.trakt.tv/oauth/device/token","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":380,"wires":[["eefe8e7.4fc947"]]},{"id":"eefe8e7.4fc947","type":"switch","z":"c53ac113.a919d","name":"200?","property":"statusCode","propertyType":"msg","rules":[{"t":"neq","v":"200","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":750,"y":380,"wires":[["44c911d2.e31f6"],["dced2541.b57b08","8f1ad54c.2a64c8"]],"outputLabels":["Not 200","200: Success!"]},{"id":"dced2541.b57b08","type":"change","z":"c53ac113.a919d","name":"Stop Loop","rules":[{"t":"set","p":"payload","pt":"msg","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":320,"wires":[["af930e5a.7e99c"]]},{"id":"b9f4ccd8.32dbb","type":"switch","z":"c53ac113.a919d","name":"Has Token?","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"access_token","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":910,"y":440,"wires":[["809c93cb.89f56"]]},{"id":"809c93cb.89f56","type":"change","z":"c53ac113.a919d","name":"","rules":[{"t":"set","p":"#:(fs)::trakt_token","pt":"flow","to":"payload.access_token","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"token set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":1015,"y":440,"wires":[["816a5370.82b7a"]],"icon":"font-awesome/fa-save","l":false},{"id":"816a5370.82b7a","type":"debug","z":"c53ac113.a919d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"\"Saved token\"","statusType":"jsonata","x":895,"y":500,"wires":[],"l":false},{"id":"3ee0a05.2da6b6","type":"inject","z":"c53ac113.a919d","name":"Stop Checking for Token","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"stop","payloadType":"str","x":230,"y":340,"wires":[["af930e5a.7e99c"]]},{"id":"af930e5a.7e99c","type":"looptimer","z":"c53ac113.a919d","duration":"5","units":"Second","maxloops":"120","maxtimeout":"3","maxtimeoutunits":"Minute","name":"Token Poll Loop","x":280,"y":260,"wires":[["2be43d9c.33a672"],[]]},{"id":"44c911d2.e31f6","type":"change","z":"c53ac113.a919d","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"waiting on user","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":915,"y":340,"wires":[["f0b4d2c.b7e833"]],"l":false},{"id":"c4110105.5f0cb","type":"inject","z":"c53ac113.a919d","name":"Inject","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"token in storage","payload":"","payloadType":"date","x":135,"y":480,"wires":[["e14c28c3.47a468"]],"l":false},{"id":"e14c28c3.47a468","type":"change","z":"c53ac113.a919d","name":"Show Token","rules":[{"t":"set","p":"payload","pt":"msg","to":"#:(fs)::trakt_token","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":250,"y":480,"wires":[["50289ceb.9b26d4"]]},{"id":"50289ceb.9b26d4","type":"debug","z":"c53ac113.a919d","name":"Your Token","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":430,"y":480,"wires":[],"icon":"font-awesome/fa-key"},{"id":"8f1ad54c.2a64c8","type":"json","z":"c53ac113.a919d","name":"Convert Payload to JSON","property":"payload","action":"obj","pretty":false,"x":795,"y":440,"wires":[["b9f4ccd8.32dbb"]],"l":false},{"id":"fefec6da.b7ce58","type":"inject","z":"c53ac113.a919d","name":"My Shows: Next 3 Days","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":560,"wires":[["28eba321.de9aac"]]},{"id":"fc1ebf09.1551e","type":"http request","z":"c53ac113.a919d","name":"Call API","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":660,"y":580,"wires":[["662cd6b.b6c8328"]]},{"id":"4f22be6b.5a605","type":"debug","z":"c53ac113.a919d","name":"Here are your shows!","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"\"Shows in Payload\"","statusType":"jsonata","x":900,"y":580,"wires":[]},{"id":"662cd6b.b6c8328","type":"json","z":"c53ac113.a919d","name":"Ensure JSON object","property":"payload","action":"obj","pretty":false,"x":755,"y":580,"wires":[["4f22be6b.5a605"]],"l":false},{"id":"1c4d429b.42222d","type":"comment","z":"c53ac113.a919d","name":"General Notes! Click Me!","info":"Generate an application here!\n[https://trakt.tv/oauth/applications/](https://trakt.tv/oauth/applications/)\n\nFull API docs:\n[https://trakt.docs.apiary.io/#reference/authentication-devices/authorize-application](https://trakt.docs.apiary.io/#reference/authentication-devices/authorize-application)\n\nNode-RED Requirements:\n- node-red-contrib-looptimer (not sure if included by default)\n\nI use file based Flow variables (to they survice restarts), so when you see 'fs' when getting or setting flow values, [this is what that is about](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115).","x":250,"y":100,"wires":[]},{"id":"d845e73.134f518","type":"comment","z":"c53ac113.a919d","name":"Messaging Comments! Click Me!","info":"Change this to match whatever you use for messaging.\n\nI personally use an MQTT call within Node-RED that calls nodes another flow that creates Telegram messages via this palette: [node-red-contrib-telegrambot-home](https://flows.nodered.org/node/node-red-contrib-telegrambot-home)","x":830,"y":260,"wires":[]},{"id":"2be43d9c.33a672","type":"function","z":"c53ac113.a919d","name":"Build HTTP Request","func":"\nreturn {\n \"headers\": { \"content-type\": \"application/x-www-form-urlencoded\" },\n \"payload\": {\n \"client_id\": flow.get('trakt_client_id', 'fs'), \n \"client_secret\": flow.get('trakt_client_secret', 'fs'), \n \"code\": msg.payload.device_code \n }\n};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":455,"y":380,"wires":[["72cbc7af.38bbc8"]],"l":false},{"id":"56cd3e91.6903c","type":"function","z":"c53ac113.a919d","name":"Build HTTP Request","func":"\nlet dt = (new Date()).toISOString();\n\nreturn {\n \"headers\": {\n \"Content-type\": \"application/json\",\n \"trakt-api-key\": flow.get('trakt_client_id', 'fs'), \n \"trakt-api-version\": 2, \n \"Authorization\": \"Bearer \" + flow.get('trakt_token', 'fs')\n },\n \"url\": \"https://api.trakt.tv/calendars/my/shows/\" + dt.split(\"T\")[0] + \"/\" + (msg.payload.days || 3)\n};\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":555,"y":580,"wires":[["fc1ebf09.1551e"]],"l":false},{"id":"96bf6703.d41748","type":"change","z":"c53ac113.a919d","name":"Setup Call","rules":[{"t":"set","p":"#:(fs)::trakt_client_id","pt":"flow","to":"YOUR CLIENT ID HERE","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"{ \"client_id\": \"YOUR CLIENT ID HERE\" }","tot":"json"},{"t":"set","p":"#:(fs)::trakt_client_secret","pt":"flow","to":"YOUR CLIENT SECRET HERE","tot":"str"},{"t":"set","p":"url","pt":"msg","to":"https://api.trakt.tv/oauth/device/code","tot":"str"},{"t":"set","p":"headers","pt":"msg","to":"{\"Content-Type\":\"application/x-www-form-urlencoded\"}","tot":"json"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":160,"wires":[["65039fcb.ef24e"]]},{"id":"5c94379a.9d80c8","type":"inject","z":"c53ac113.a919d","name":"My Shows: Next 7 Days","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{ \"days\": 7 }","payloadType":"json","x":180,"y":600,"wires":[["28eba321.de9aac"]]},{"id":"28eba321.de9aac","type":"switch","z":"c53ac113.a919d","name":"Check for token","property":"#:(fs)::trakt_token","propertyType":"flow","rules":[{"t":"istype","v":"string","vt":"string"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":400,"y":580,"wires":[["56cd3e91.6903c"],["61df4db4.d67754"]]},{"id":"61df4db4.d67754","type":"debug","z":"c53ac113.a919d","name":"","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"\"No token stored\"","statusType":"jsonata","x":555,"y":640,"wires":[],"l":false},{"id":"4d014701.54d318","type":"comment","z":"c53ac113.a919d","name":"Check Token","info":"Click this to look for token in flow variable","x":190,"y":440,"wires":[]},{"id":"8f244f69.74793","type":"comment","z":"c53ac113.a919d","name":"Test!","info":"","x":370,"y":640,"wires":[]},{"id":"333d0b5c.8ed1e4","type":"mqtt-broker","name":"My Mosquitto Broker","broker":"192.168.0.107","port":"1883","clientid":"","usetls":true,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","willTopic":"","willQos":"0","willRetain":"false","willPayload":""}]
@MorningZ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment