Last active
June 25, 2020 15:38
-
-
Save davidsharp/de9fcd20abeb03c529da6f37e25a9ca7 to your computer and use it in GitHub Desktop.
bitbar plugin that naively grabs package tracking info from package.place
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env /usr/local/bin/node | |
| const bitbar = require('bitbar'); | |
| const http = require('https'); | |
| // change me for your package | |
| const package = '188946163494' | |
| let provider = null //'FEDEX' | |
| http.get(`https://package.place/api/track/${package}`, (res) => { | |
| const { statusCode } = res; | |
| const contentType = res.headers['content-type']; | |
| let error; | |
| if (statusCode !== 200) { | |
| error = new Error('Request Failed.\n' + | |
| `Status Code: ${statusCode}`); | |
| } else if (!/^application\/json/.test(contentType)) { | |
| error = new Error('Invalid content-type.\n' + | |
| `Expected application/json but received ${contentType}`); | |
| } | |
| if (error) { | |
| console.error(error.message); | |
| // Consume response data to free up memory | |
| res.resume(); | |
| return; | |
| } | |
| res.setEncoding('utf8'); | |
| let rawData = ''; | |
| res.on('data', (chunk) => { rawData += chunk; }); | |
| res.on('end', () => { | |
| try { | |
| const parsedData = JSON.parse(rawData); | |
| const data = parsedData; | |
| if(!provider)provider=Object.keys(data)[0]; | |
| bitbar([ | |
| {text: `📦`}, | |
| bitbar.separator, | |
| ...data[provider].map(({timestamp,location,status})=>( | |
| {text: `${(new Date(timestamp)).toString()} – ${status} – ${location}`} | |
| )), | |
| bitbar.separator, | |
| ...Object.keys(data).map(prov=>({text:prov})), | |
| //{text:JSON.stringify(process.argv), bash:'/usr/local/bin/node', param1:process.argv[1], param2:'open', terminal:false} | |
| ]) | |
| } catch (e) { | |
| console.error(e.message); | |
| } | |
| }); | |
| }).on('error', (e) => { | |
| console.error(`Got error: ${e.message}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment