Skip to content

Instantly share code, notes, and snippets.

@cbguder
Created November 15, 2016 04:05
Show Gist options
  • Save cbguder/9862d5310f2e1862924c1f18334ae2c3 to your computer and use it in GitHub Desktop.
Save cbguder/9862d5310f2e1862924c1f18334ae2c3 to your computer and use it in GitHub Desktop.
Group Foursquare bookmarks into lists by city
source "https://rubygems.org"
gem "rest-client"
gem "awesome_print"
#!/usr/bin/env ruby
require "rest-client"
require "json"
require "awesome_print"
TOKEN = "<OAUTH TOKEN>"
BASE_URL = "https://api.foursquare.com/v2"
PAGE_SIZE = 200
BASE_PARAMS = {
oauth_token: TOKEN,
v: "20161112",
m: "foursquare"
}
def main
lists = get_all_created_lists("self")
todos_list = get_list("self/todos", limit: 1)
todos_list_id = todos_list["response"]["list"]["id"]
items = get_all_items_in_list("self/todos")
items.each do |item|
venue = item["venue"]
unless venue
puts "Skipping item due to missing venue"
next
end
city = get_city_name(item)
unless city
puts "Skipping item due to missing city"
next
end
list = lists.find {|l| l["name"] == city}
if list == nil
list = add_list(city)["response"]["list"]
lists << list
end
begin
add_item(list["id"], venue["id"])
delete_item("self/todos", venue["id"])
rescue => e
ap e.response.headers
error = JSON.parse(e.response.body)
ap error
exit(false) unless error["meta"]["errorDetail"].end_with?("already on this list")
end
end
end
def get_city_name(item)
venue = item["venue"]
location = venue["location"] if venue
city = location["city"] if location
city == "" ? nil : city
end
def get_all_created_lists(user_id)
items = []
loop do
lists = get_lists(user_id, group: 'created', offset: items.count, limit: PAGE_SIZE)
total_count = lists["response"]["lists"]["count"]
items += lists["response"]["lists"]["items"]
return items if items.count == total_count
end
end
def get_all_items_in_list(list_id)
items = []
loop do
list = get_list(list_id, offset: items.count, limit: PAGE_SIZE)
total_count = list["response"]["list"]["listItems"]["count"]
items += list["response"]["list"]["listItems"]["items"]
return items if items.count == total_count
end
end
def get_lists(user_id, params = {})
puts "Getting lists (offset #{params[:offset]})..."
get_json("/users/#{user_id}/lists", params)
end
def get_list(list_id, params = {})
puts "Getting list items (offset #{params[:offset]})..."
get_json("/lists/#{list_id}", params)
end
def add_list(list_name)
puts "Creating list #{list_name}"
post_json("/lists/add", name: list_name)
end
def add_item(list_id, venue_id)
puts "Adding venue #{venue_id} to list #{list_id}"
post_json("/lists/#{list_id}/additem", venueId: venue_id)
end
def delete_item(list_id, venue_id)
puts "Deleting venue #{venue_id} from list #{list_id}"
post_json("/lists/#{list_id}/deleteitem", venueId: venue_id)
end
def get_json(path, params = {})
params = BASE_PARAMS.merge(params)
response = RestClient.get(BASE_URL + path, params: params)
JSON.parse(response.body)
end
def post_json(path, params = {})
params = BASE_PARAMS.merge(params)
response = RestClient.post(BASE_URL + path, params)
JSON.parse(response.body)
end
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment