Created
October 31, 2015 15:55
instagram example
This file contains 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
# @var {Object} bound - Meteor.bindEnvironment aka Fiber wrapper | |
bound = Meteor.bindEnvironment (callback) -> return callback() | |
ServiceConfiguration.configurations.remove service: "instagram" | |
ServiceConfiguration.configurations.insert | |
service: "instagram" | |
clientId: "clientId" | |
requestPermissions: ['basic', 'likes', 'relationships', 'comments'] | |
scope: ['basic', 'likes', 'relationships', 'comments'] | |
secret: "secret" | |
ig = instagramNode.instagram() | |
ig.use | |
client_id: 'client_id' | |
client_secret: 'client_secret' | |
# Function which fetch recently posted photos and videos of users, who logged in via Instagram | |
getInsta = -> | |
# We look for users who logged in via Insragram | |
# @toRead https://atmospherejs.com/bozhao/accounts-instagram | |
users = Meteor.users.find 'services.instagram.accessToken': $exists: true | |
if users.count() > 0 | |
users.forEach (user) -> | |
err = false | |
if user.profile.settings and user.profile.settings.tags | |
ig.use access_token: user.services.instagram.accessToken | |
# Search by tag | |
tag = 'MyTag' | |
# Assuming we store "tagdata" to fetch tags we need | |
# and avoid double fetched items | |
tagdata = Collections.tags.findOne(tagname: tag) or {} | |
min_tag_id = if (tagdata) then tagdata.min_tag_id else 0 | |
ig.tag_media_recent tag, min_tag_id: min_tag_id, (err, medias, pagination, remaining, limit) -> bound -> | |
if err | |
Meteor.users.update | |
_id: user._id | |
, | |
$unset: 'services.instagram.accessToken': '' | |
$set: 'services.resume.loginTokens': [] | |
console.log "User's \"#{user.profile.name}\" access_token was expired" | |
else | |
_.each medias, (media) -> bound -> | |
media.created_at = new Date() | |
Collections.tags.upsert | |
tagname: tag | |
, | |
$set: | |
min_tag_id: pagination.min_tag_id | |
tagname: tag | |
upsert = Collections.if.upsert id: media.id, media | |
if upsert.insertedId | |
# Use file collection to fetch and store images | |
# @toRead https://atmospherejs.com/ostrio/files | |
Collections.IGPhotos.load media.images.standard_resolution.url | |
, | |
name: "#{moment.unix(media.created_time).format('DD-MM-YYYY')}_@#{media.user.username}_#{media.id}.jpg" | |
meta: | |
media: media | |
ifId: upsert.insertedId | |
if media.type is 'video' | |
# Use file collection to fetch and store videos | |
# @toRead https://atmospherejs.com/ostrio/files | |
Collections.IGPhotos.load media.videos.standard_resolution.url | |
, | |
name: "#{moment.unix(media.created_time).format('DD-MM-YYYY')}_@#{media.user.username}_#{media.id}.mp4" | |
meta: | |
media: media | |
ifId: upsert.insertedId |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment