Created
June 2, 2014 17:47
-
-
Save joebalancio/d97feda6b76aa28197e5 to your computer and use it in GitHub Desktop.
Quick project to determine how many hashtags an Instagram user has used. First grab all posts by user, then process posts and perform hashtag calculation.
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
fs = require 'fs' | |
files = fs.readdirSync "#{process.cwd()}/json" | |
dataMap = {} | |
for file in files | |
raw = require "#{process.cwd()}/json/#{file}" | |
for x in raw.data | |
dataMap[x.id] = x | |
tagMap = {} | |
count = 0 | |
for key, val of dataMap | |
count += 1 | |
for t in val.tags | |
if tagMap[t] | |
tagMap[t] += 1 | |
else | |
tagMap[t] = 1 | |
console.log "Processed #{count} records" | |
tagCounts = [] | |
for tag, count of tagMap | |
if tagCounts[count] | |
tagCounts[count].push(tag) | |
else | |
tagCounts[count] = [tag] | |
module.exports.tagMap = tagMap | |
module.exports.tagCounts = tagCounts | |
module.exports.printCounts = (counts) -> | |
for index, val of counts | |
if val | |
console.log(index, "counts of", val) | |
return null |
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
request = require 'request' | |
fs = require 'fs' | |
crypto = require 'crypto' | |
md5 = crypto.createHash 'md5' | |
options = | |
url: "https://api.instagram.com/v1/users/#{process.env.USER_ID}/media/recent" | |
qs: | |
access_token: process.env.ACCESS_TOKEN | |
count: 1000 | |
json: true | |
count = 0 | |
processData = (data) -> | |
console.log 'length', data.data.length | |
fs.writeFileSync "#{process.cwd()}/json/#{data.data[0].id}.json", JSON.stringify(data) | |
handleResponse = (err, response, body) -> | |
if count is 1000 | |
return | |
if not err | |
processData body | |
count += 1 | |
options = | |
url: body.pagination.next_url | |
json: true | |
setTimeout (-> | |
request options, handleResponse | |
), 5000 | |
request options, handleResponse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment