Integrating Swarm Check-Ins with Micro.blog (part 2, Daily)
The following script is similar to what I posted earlier for @settee; but rather than post immediately from a webhook, it compiles a daily list that is pushed to the blog every day at whatever time the user schedules it for (I set up a trigger for daily between midnight and 1 AM) for the preceding day—a table of public checkins followed by a Google static map of those locations.
For privacy reasons, both scripts exclude checkins that are marked as “off the grid” ("visibility": "private"
in the JSON feed).
This script also makes use of the moment.js
date/time parsing/display library using an eval()
statement, which is a way Google Apps Script can be made to work with outside JavaScript libraries. Not all libraries work; moment.js
happens to.
function dailySwarm(e) {
// Add your values below for the Swarm and Micro.Blog tokens.
var swarmtoken = "XXX";
var microblogtoken = "XXX"
eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js').getContentText()) // moment.js library
var startTime = moment().startOf('day').subtract(1, 'days').unix();
var endTime = moment().endOf('day').subtract(1, 'days').unix();
var data = JSON.parse(
UrlFetchApp.fetch(
`https://api.foursquare.com/v2/users/self/checkins?afterTimestamp=${startTime}&beforeTimestamp=${endTime}&limit=250&v=20241001&oauth_token=${swarmtoken}`
)
);
var checkinmap = Maps.newStaticMap().setSize(600, 600).setFormat(Maps.StaticMap.Format.PNG);
if (data.response.checkins.items.length > 0) {
var text = '| Timestamp | Venue | Category | Location |\n';
text += '|----------:|-------|----------|----------|\n';
for (i = 0; i < data.response.checkins.items.length; i++) {
var checkin = data.response.checkins.items[i];
if (checkin.visibility != 'private') {
checkinmap.addMarker(checkin.venue.location.lat, checkin.venue.location.lng);
var cityState = [checkin.venue.location.city, checkin.venue.location.state].join(', ')
text += `| ${moment.unix(checkin.createdAt).format('LT')} | ${checkin.venue.name} | ${checkin.venue.categories[0].name} | ${cityState} |\n`
}
}
var checkinmapupload = JSON.parse(UrlFetchApp.fetch(
"https://micro.blog/micropub/media", {
"method": "POST",
"headers": {
"Authorization": `Bearer ${microblogtoken}`
},
"payload": {
"file": checkinmap.getBlob()
}
}
).getContentText());
console.log(checkinmapupload);
var payload = {
"type": ["h-entry"],
"properties": {
"published": [moment().startOf('day').format()],
"name": `🗺️ Swarm Checkin Log: ${moment().startOf('day').subtract(1, 'days').format('LL')}`,
"content": [text],
"photo": [checkinmapupload.url],
}
};
}
else {
var payload = {
"type": ["h-entry"],
"properties": {
"published": [moment().startOf('day').format()],
"name": `🗺️ Swarm Checkin Log: ${moment().startOf('day').subtract(1, 'days').format('LL')}`,
"content": ["No public checkins on this date."]
}
};
}
var request = UrlFetchApp.fetch(
"https://micro.blog/micropub", {
"method": "POST",
"headers": {
"Authorization": `Bearer ${microblogtoken}`
},
"payload": JSON.stringify(payload)
}
);
console.log(request.getContentText());
}