Skip to content

Instantly share code, notes, and snippets.

@axtutuu
Last active November 24, 2016 07:09
Show Gist options
  • Save axtutuu/85430e604ffc95ae051bf22b3105f73a to your computer and use it in GitHub Desktop.
Save axtutuu/85430e604ffc95ae051bf22b3105f73a to your computer and use it in GitHub Desktop.
tweet取得
require 'active_support'
require 'active_support/core_ext'
require 'json'
require 'oauth'
require 'pry'
require "dotenv"
Dotenv.load
consumer_key = ENV["CONSUMER_KEY"]
consumer_secret = ENV["CONSUMER_SECRET"]
access_token = ENV["ACCESS_TOKEN"]
access_token_secret = ENV["ACCESS_TOKEN_SECRET"]
consumer = OAuth::Consumer.new(
consumer_key,
consumer_secret,
site:'https://api.twitter.com/'
)
endpoint = OAuth::AccessToken.new(consumer, access_token, access_token_secret)
# name = "Pocke777"
# max_id = "682136652421050368"
max_id = ""
name = "ujiqn"
# GET
params = { screen_name: name, count: 200 }
hash = []
begin
# tweet取得
params.merge!({ max_id: max_id }) unless max_id.empty?
response = endpoint.get("https://api.twitter.com/1.1/statuses/user_timeline.json?#{params.to_query}")
# hashの追加
JSON.parse(response.body).each do |j|
hash << { id: j["id_str"], text: j["text"], created_at: j["created_at"] }
end
max_id = hash.last[:id]
p max_id
end while JSON.parse(response.body).length > 2
# ファイルに書き込み
File.open("json.txt", "a") do |f|
f.puts hash
end
# frozen_string_literal: true
source "https://rubygems.org"
# gem "rails"
gem "oauth"
gem "dotenv"
gem "pry"
# for active_support/core_ext
gem "rails"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment