Skip to content

Instantly share code, notes, and snippets.

@rono23
Last active May 16, 2021 06:32
Show Gist options
  • Select an option

  • Save rono23/f47d1529dcee7cc2de19b44f71acc33f to your computer and use it in GitHub Desktop.

Select an option

Save rono23/f47d1529dcee7cc2de19b44f71acc33f to your computer and use it in GitHub Desktop.

Revisions

  1. rono23 revised this gist May 16, 2021. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion checkins.rb
    Original file line number Diff line number Diff line change
    @@ -96,7 +96,7 @@
    "End Date" => start_date,
    "End Time" => start_time,
    "All Day Event" => "False",
    "Description" => "Swarm",
    "Description" => item["shout"],
    "Location" => address,
    "Private" => "False"
    }
    4 changes: 2 additions & 2 deletions output.csv
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
    JR Shinjuku Station (JR 新宿駅),9/12/2019,13:23 PM,9/12/2019,13:23 PM,False,"Swarm",東京都新宿区新宿3-38-1,False
    Dublin Connolly Railway Station,11/15/2018,18:23 PM,11/15/2018,18:23 PM,False,"Swarm","Amiens St, Dublin, Dublin City, Ireland",False
    JR Shinjuku Station (JR 新宿駅),9/12/2019,13:23 PM,9/12/2019,13:23 PM,False,"コメント",東京都新宿区新宿3-38-1,False
    Dublin Connolly Railway Station,11/15/2018,18:23 PM,11/15/2018,18:23 PM,False,"Shout","Amiens St, Dublin, Dublin City, Ireland",False
  2. rono23 revised this gist May 14, 2021. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions checkins.rb
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,17 @@
    # gem installl oauth2
    ## Usage
    # gem install oauth2
    # ruby checkins.rb
    #
    # .env
    ## .env
    # https://foursquare.com/developers/apps
    # ENV["4SQ_CLIENT_ID"] = ""
    # ENV["4SQ_CLIENT_SECRET"] = ""
    # ENV["4SQ_REDIRECT_URI"] = "http://localhost"
    #
    ## Save access_token after running this script
    # ENV["4SQ_ACCESS_TOKEN"] = ""
    #
    # Import CSV to Google Calendar
    ## Import CSV to Google Calendar
    # https://calendar.google.com/calendar/u/0/r/settings/export

    require "oauth2"
    @@ -18,9 +20,9 @@

    client_id = ENV["4SQ_CLIENT_ID"]
    client_secret = ENV["4SQ_CLIENT_SECRET"]
    redirect_uri = ENV["4SQ_REDIRECT_URI"]
    access_token = ENV["4SQ_ACCESS_TOKEN"]
    csv_path = "./#{Time.now.strftime("%Y%m%d%H%M")}.csv"
    redirect_uri = "http://localhost"
    accept_language = "ja",
    client = OAuth2::Client.new(client_id, client_secret, {
    site: "https://foursquare.com",
    @@ -43,7 +45,7 @@
    items = []

    loop do
    puts "Fetching.. #{offset}"
    puts "Fetching: #{offset}..#{offset + limit}"

    response = client.request(
    :get,
    @@ -59,12 +61,8 @@

    checkins = response.parsed.dig("response", "checkins")
    items += checkins["items"]

    if checkins["count"] < offset
    break
    else
    offset += limit
    end
    offset += limit
    break if checkins["count"] < offset
    end

    rows = items.map do |item|
  3. rono23 revised this gist May 14, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion checkins.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    # ENV["4SQ_CLIENT_ID"] = ""
    # ENV["4SQ_CLIENT_SECRET"] = ""
    #
    ## Save access_token after running this script
    ## Save access_token after running this script
    # ENV["4SQ_ACCESS_TOKEN"] = ""
    #
    # Import CSV to Google Calendar
  4. rono23 revised this gist May 14, 2021. 2 changed files with 3 additions and 3 deletions.
    2 changes: 1 addition & 1 deletion checkins.rb
    Original file line number Diff line number Diff line change
    @@ -98,7 +98,7 @@
    "End Date" => start_date,
    "End Time" => start_time,
    "All Day Event" => "False",
    "Description" => "",
    "Description" => "Swarm",
    "Location" => address,
    "Private" => "False"
    }
    4 changes: 2 additions & 2 deletions output.csv
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,3 @@
    Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
    JR Shinjuku Station (JR 新宿駅),9/12/2019,13:23 PM,9/12/2019,13:23 PM,False,"",東京都新宿区新宿3-38-1,False
    Dublin Connolly Railway Station,11/15/2018,18:23 PM,11/15/2018,18:23 PM,False,"","Amiens St, Dublin, Dublin City, Ireland",False
    JR Shinjuku Station (JR 新宿駅),9/12/2019,13:23 PM,9/12/2019,13:23 PM,False,"Swarm",東京都新宿区新宿3-38-1,False
    Dublin Connolly Railway Station,11/15/2018,18:23 PM,11/15/2018,18:23 PM,False,"Swarm","Amiens St, Dublin, Dublin City, Ireland",False
  5. rono23 created this gist May 14, 2021.
    110 changes: 110 additions & 0 deletions checkins.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    # gem installl oauth2
    # ruby checkins.rb
    #
    # .env
    # https://foursquare.com/developers/apps
    # ENV["4SQ_CLIENT_ID"] = ""
    # ENV["4SQ_CLIENT_SECRET"] = ""
    #
    ## Save access_token after running this script
    # ENV["4SQ_ACCESS_TOKEN"] = ""
    #
    # Import CSV to Google Calendar
    # https://calendar.google.com/calendar/u/0/r/settings/export

    require "oauth2"
    require "csv"
    begin load ".env"; rescue LoadError; nil; end

    client_id = ENV["4SQ_CLIENT_ID"]
    client_secret = ENV["4SQ_CLIENT_SECRET"]
    access_token = ENV["4SQ_ACCESS_TOKEN"]
    csv_path = "./#{Time.now.strftime("%Y%m%d%H%M")}.csv"
    redirect_uri = "http://localhost"
    accept_language = "ja",
    client = OAuth2::Client.new(client_id, client_secret, {
    site: "https://foursquare.com",
    authorize_url: "oauth2/authenticate",
    token_url: "oauth2/access_token"
    })

    if access_token.nil? || access_token.empty?
    authorize_url = client.auth_code.authorize_url(redirect_uri: redirect_uri)
    puts "Open url below and copy a code from callback url:\n#{authorize_url}"
    puts "Paste the code:"
    code = gets.chomp
    token = client.auth_code.get_token(code, redirect_uri: redirect_uri)
    access_token = token.token
    puts "Save access_token if you want: #{access_token}"
    end

    offset = 0
    limit = 250
    items = []

    loop do
    puts "Fetching.. #{offset}"

    response = client.request(
    :get,
    "https://api.foursquare.com/v2/users/self/checkins",
    params: {
    v: 20210514,
    limit: limit,
    offset: offset,
    oauth_token: access_token,
    headers: { "Accept-Language" => accept_language }
    }
    )

    checkins = response.parsed.dig("response", "checkins")
    items += checkins["items"]

    if checkins["count"] < offset
    break
    else
    offset += limit
    end
    end

    rows = items.map do |item|
    created_at = Time.at(item["createdAt"] + item["timeZoneOffset"] * 60, in: "UTC")
    start_date = created_at.strftime("%-m/%-d/%Y")
    start_time = created_at.strftime("%H:%M %p")
    venue = item.dig("venue")
    location = venue["location"]
    formattedAddress = location["formattedAddress"] || []

    if location["cc"] == "JP"
    case formattedAddress.size
    when 2
    index = -1
    when 3
    index = -2
    end

    if index
    address = formattedAddress[index].split(", ").reverse.join
    address += formattedAddress[0]
    end
    else
    address = formattedAddress.join(", ")
    end

    {
    "Subject" => venue["name"],
    "Start Date" => start_date,
    "Start Time" => start_time,
    "End Date" => start_date,
    "End Time" => start_time,
    "All Day Event" => "False",
    "Description" => "",
    "Location" => address,
    "Private" => "False"
    }
    end

    CSV.open(csv_path, "wb", converters: nil) do |csv|
    csv << rows.first.keys
    rows.each { |row| csv << row.values }
    end
    3 changes: 3 additions & 0 deletions output.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description,Location,Private
    JR Shinjuku Station (JR 新宿駅),9/12/2019,13:23 PM,9/12/2019,13:23 PM,False,"",東京都新宿区新宿3-38-1,False
    Dublin Connolly Railway Station,11/15/2018,18:23 PM,11/15/2018,18:23 PM,False,"","Amiens St, Dublin, Dublin City, Ireland",False