Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Last active December 16, 2015 18:49
Show Gist options
  • Save rummelonp/5480316 to your computer and use it in GitHub Desktop.
Save rummelonp/5480316 to your computer and use it in GitHub Desktop.
Vine の非公式 API を Ruby から使うやつ (参考: https://github.com/starlock/vino/wiki/API-Reference)
# -*- coding: utf-8 -*-
require 'faraday'
require 'faraday_middleware'
module Vine
def self.login(username = ENV['VINE_USERNAME'], password = ENV['VINE_PASSWORD'])
data = Vine::Client.new.login(username, password)
Vine::Client.new(data['key'])
end
module API
def login(username, password)
post('/users/authenticate', :username => username, :password => password)
end
def logout
delete('/users/authenticate')
end
def popular
get('/timelines/popular')
end
def profile(user_id = nil)
if user_id
get("/users/profiles/#{user_id}")
else
get('/users/me')
end
end
def timeline(user_id)
get("/timelines/users/#{user_id}")
end
def tag(tag)
get("/timelines/tags/#{tag}")
end
def notifications(user_id)
get("/users/#{user_id}/pendingNotificationsCount")
end
end
class Client
include API
def initialize(key = nil)
@key = key
end
def get(path, params = {})
request(:get, path, params)
end
def post(path, params = {})
request(:post, path, params)
end
def put(path, params = {})
request(:put, path, params)
end
def delete(path, params = {})
request(:delete, path, params)
end
private
def request(http_method, path, params = {})
response = connection.send(http_method.to_sym, path, params) do |request|
request[:vine_session_id] = @key if @key
end
response.body['data']
end
def connection
options = {
:url => 'https://api.vineapp.com/',
:headers => {
:accept => 'application/json',
:user_agent => "com.vine.iphone/1.0.3 (unknown, iPhone OS 6.1.0, iPhone, Scale/2.000000)",
},
:request => {
:open_timeout => 5,
:timeout => 10,
},
:ssl => {
:verify => true
},
}
Faraday.new(options) do |builder|
builder.response :raise_error
builder.response :mashify
builder.response :json
builder.request :url_encoded
builder.adapter :net_http
end
end
end
class VineError < StandardError
end
module Response
class RaiseError < Faraday::Response::Middleware
def on_complete(env)
case env[:status].to_i
when 400...600
raise Vine::VineError.new(error_message(env))
end
end
private
def error_message(env)
[
env[:method].to_s.upcase,
env[:url].to_s,
env[:status],
error_body(env[:body])
].join(': ')
end
def error_body(body)
if body.nil?
nil
elsif body['error']
body['error']
end
end
end
end
Faraday.register_middleware :response, :raise_error => lambda { Vine::Response::RaiseError }
end
@rummelonp
Copy link
Author

$ pry
require './vine'
client = Vine::login 'mail', 'password'
user_id = client.profile.userId
posts = client.timeline(user_id)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment