Skip to content

Instantly share code, notes, and snippets.

@rickbox
Created April 16, 2019 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickbox/7a21c360173a033890a094fbf47baa6e to your computer and use it in GitHub Desktop.
Save rickbox/7a21c360173a033890a094fbf47baa6e to your computer and use it in GitHub Desktop.
twitterAPI_sample2
class UsersController < ApplicationController
#before_action :set_user, only: [:show, :update, :destroy]
before_action :set_twitter_client
def set_twitter_client
omniauth = request.env['omniauth.auth']
@client = Twitter::REST::Client.new do |config|
config.consumer_key = ENV['CONSUMER_KEY']
config.consumer_secret = ENV['CONSUMER_SECRET']
config.access_token = omniauth.credentials.token
config.access_token_secret = omniauth.credentials.secret
end
end
# GET /users
def index
@users = User.all
render json: @users
end
# GET /users/1
def show
render json: @user
end
# POST /users
def create
user_name = request.env['omniauth.auth']['info']['nickname']
if User.exists?(name: user_name)
@user = User.find_by(name: user_name)
@user.movies.purge
FileUtils.rm_rf(Dir.glob('./storage/*'))
#@user.movies.purge_later
m3u8_num = 0
m3u8_arr = []
@n = 1
@user = User.find_by(name: user_name)
@old_since_id = @user.since_id
@client.user_timeline(count: 1,since_id: @old_since_id).each do |timeline|
if @n == 1
@user.since_id = timeline.id
@n = 2
end
@last_id = timeline.id
if timeline.media.to_s.include?("Video")
i = 0
for i in 0..3
if timeline.media[0].video_info.variants[i].url.to_s.include?(".m3u8") then
m3u8_arr[m3u8_num] = timeline.media[0].video_info.variants[i].url.to_s
m3u8_num += 1
break
else
i += 1
end
end
end
end
if @old_since_id != @user.since_id
for n in 1..2
@client.user_timeline(count: 1,since_id: @old_since_id, max_id: @last_id - 1).each do |timeline|
@last_id = timeline.id
if timeline.media.to_s.include?("Video")
for i in 0..3
if timeline.media[0].video_info.variants[i].url.to_s.include?(".m3u8") then
m3u8_arr[m3u8_num] = timeline.media[0].video_info.variants[i].url.to_s
m3u8_num += 1
break
else
i += 1
end
end
end
end
end
end
# 1つ目のm3u8をDL
require 'open-uri'
user_video_num_old = @user.video_num
m3u8_arr.each do |m3u8_url|
open(m3u8_url) do |file|
open(@user.video_num.to_s << "_m3u8", "w+b") do |out|
out.write(file.read)
end
@user.video_num += 1
end
end
# 1つ目のm3u8情報から2つ目のm3u8のURLを取得し、DL
for m3u8_file_num_all in user_video_num_old..@user.video_num - 1
m3u8_url2 = IO.readlines(m3u8_file_num_all.to_s << "_m3u8")[-1]
m3u8_url2 = "https://video.twimg.com" << m3u8_url2
m3u8_url2.chomp!
open(m3u8_url2) do |file_m3u8|
open(m3u8_file_num_all.to_s << "_m3u8", "w+b") do |out|
out.write(file_m3u8.read)
end
end
@user.movies.attach({ io: File.open("#{Rails.root}/#{m3u8_file_num_all}_m3u8"), filename: "#{@user.name}-m3u8_#{m3u8_file_num_all}", content_type: "application/vnd.apple.mpegurl" })
# 2つ目のm3u8情報からtsのURLを取得し、DL
ts_num = 0
File.foreach("#{Rails.root}/#{m3u8_file_num_all}_m3u8"){|line|
line.chomp!
if line[-3,3] == ".ts" then
ts_url = "https://video.twimg.com" << line
open(ts_url) do |file_ts|
open(m3u8_file_num_all.to_s<< "_" << ts_num.to_s << "_ts", "w+b") do |out|
out.write(file_ts.read)
end
end
@user.movies.attach({ io: File.open("#{Rails.root}/#{m3u8_file_num_all}_#{ts_num}_ts"), filename: "#{@user.name}-ts_#{m3u8_file_num_all}_#{ts_num}" , content_type: "video/mp2t" })
ts_num += 1
end
}
end
if @user.save
puts "成功"
#JSONで返る?
#FileUtils.rm_rf(Dir.glob('*_m3u8'))
#FileUtils.rm_rf(Dir.glob('*_ts'))
render json: @user
else
puts "失敗"
render json: @user
end
#新規############################
else
@user = User.new(name: user_name)
# 1つ目のm3u8のURLを取得
m3u8_num = 0
m3u8_arr = []
@n = 1
@client.user_timeline(count: 1).each do |timeline|
if @n == 1
@user.since_id = timeline.id
@n = 2
end
@last_id = timeline.id
if timeline.media.to_s.include?("Video")
for i in 0..3
if timeline.media[0].video_info.variants[i].url.to_s.include?(".m3u8") then
m3u8_arr[m3u8_num] = timeline.media[0].video_info.variants[i].url.to_s
m3u8_num += 1
break
else
i += 1
end
end
end
end
for n in 1..2
@client.user_timeline(count: 1,max_id: @last_id - 1).each do |timeline|
@last_id = timeline.id
if timeline.media.to_s.include?("Video")
for i in 0..3
if timeline.media[0].video_info.variants[i].url.to_s.include?(".m3u8") then
m3u8_arr[m3u8_num] = timeline.media[0].video_info.variants[i].url.to_s
m3u8_num += 1
break
else
i += 1
end
end
end
end
end
# 1つ目のm3u8をDL
#m3u8_file_num = 0
require 'open-uri'
m3u8_arr.each do |m3u8_url|
open(m3u8_url) do |file|
open(@user.video_num.to_s << "_m3u8", "w+b") do |out|
out.write(file.read)
end
@user.video_num += 1
end
end
# 1つ目のm3u8情報から2つ目のm3u8のURLを取得し、DL
for m3u8_file_num_all in 0..@user.video_num - 1
m3u8_url2 = IO.readlines(m3u8_file_num_all.to_s << "_m3u8")[-1]
m3u8_url2 = "https://video.twimg.com" << m3u8_url2
m3u8_url2.chomp!
open(m3u8_url2) do |file_m3u8|
open(m3u8_file_num_all.to_s << "_m3u8", "w+b") do |out|
out.write(file_m3u8.read)
end
end
@user.movies.attach({ io: File.open("#{Rails.root}/#{m3u8_file_num_all}_m3u8"), filename: "#{@user.name}-m3u8_#{m3u8_file_num_all}", content_type: "application/vnd.apple.mpegurl" })
# 2つ目のm3u8情報からtsのURLを取得し、DL
ts_num = 0
File.foreach("#{Rails.root}/#{m3u8_file_num_all}_m3u8"){|line|
line.chomp!
if line[-3,3] == ".ts" then
ts_url = "https://video.twimg.com" << line
open(ts_url) do |file_ts|
open(m3u8_file_num_all.to_s<< "_" << ts_num.to_s << "_ts", "w+b") do |out|
out.write(file_ts.read)
end
end
@user.movies.attach({ io: File.open("#{Rails.root}/#{m3u8_file_num_all}_#{ts_num}_ts"), filename: "#{@user.name}-ts_#{m3u8_file_num_all}_#{ts_num}" , content_type: "video/mp2t" })
ts_num += 1
end
}
end
if @user.save
puts "成功"
#JSONで返る?
#FileUtils.rm_rf(Dir.glob('*_m3u8'))
#FileUtils.rm_rf(Dir.glob('*_ts'))
render json: @user
else
puts "失敗"
render json: @user
end
end
p"最後"
#render json: { status: 'SUCCESS'}
end
# PATCH/PUT /users/1
def update
if @user.update(user_params)
render json: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end
# DELETE /users/1
def destroy
@user.destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment