Skip to content

Commit

Permalink
Merge pull request #4 from tsuwatch/fix/v2
Browse files Browse the repository at this point in the history
Support niconico search API v2
  • Loading branch information
tsuwatch authored Jul 30, 2017
2 parents 6a696ed + 3b1d5d9 commit 3c9cbf7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 86 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
FROM ruby:2.2.3
FROM ruby:2.3.0

WORKDIR /app
ADD Gemfile /app/Gemfile
ADD Gemfile.lock /app/Gemfile.lock
RUN bundle install -j4
ADD . /app
ENV LANG C.UTF-8
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ gem 'twitter'
gem 'eventmachine'
gem 'thin'
gem 'dotenv'
gem 'nicosa'
27 changes: 12 additions & 15 deletions config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,23 @@ EM::defer do
if today != yesterday
registered_tags = Ayaneru.redis.lrange 'tags', 0, -1
registered_tags.each do |tag|
r = Ayaneru.niconico.search(tag, 1).to_s.split("\n")
results = JSON.parse(r[2])
if results['values']
results['values'].each do |value|
response = Ayaneru.niconico.search(tag, 1)
response.each do |r|
begin
ret = Ayaneru.niconico.reserve(r.contentId)
rescue => exception
puts exception.message
begin
ret = Ayaneru.niconico.reserve(value["cmsid"])
rescue => exception
puts exception.message
begin
Ayaneru.twitter.create_direct_message(Ayaneru.twitter.user.screen_name, "これ以上タイムシフト予約できません.『#{value['title']}』(http://live.nicovideo.jp/watch/#{value['cmsid']})")
rescue => exception
puts exception.message
end
end
begin
Ayaneru.twitter.create_direct_message(Ayaneru.twitter.user.screen_name, "『#{value['title']}』(http://live.nicovideo.jp/watch/#{value['cmsid']})をタイムシフト予約しました.") if ret
Ayaneru.twitter.direct_message_create(Ayaneru.twitter.user.screen_name, "これ以上タイムシフト予約できません.『#{r.title}』(#{r.url})")
rescue => exception
puts exception.message
end
end
begin
Ayaneru.twitter.direct_message_create(Ayaneru.twitter.user.screen_name, "『#{r.title}』(#{r.url})をタイムシフト予約しました.") if ret
rescue => exception
puts exception.message
end
end
end
yesterday = today
Expand Down
1 change: 1 addition & 0 deletions lib/ayaneru.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'twitter'
require 'redis'
require 'dotenv'
require 'nicosa'

Dotenv.load
module Ayaneru
Expand Down
76 changes: 15 additions & 61 deletions lib/ayaneru/niconico/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,21 @@
module Ayaneru
class Niconico
def search(tag, until_days)
logout
post_data = {}
data_filters = Array.new
data_filters[0] = {
"field" => "ss_adult",
"type" => "equal",
"value" => false
}
data_filters[1] = {
"field" => "live_status",
"type" => "equal",
"value" => "reserved"
}
data_filters[2] = {
"field" => "provider_type",
"type" => "equal",
"value" => "official"
}
data_filters[3] = {
"field" => "start_time",
"from" => Time.now.strftime("%Y-%m-%d %H:%M:%S"),
"include_lower" => true,
"include_upper" => true,
"to" => Time.at(Time.now.to_i + until_days * 24 * 60 * 60).strftime("%Y-%m-%d %H:%M:%S"),
"type" => "range"
}
data_filters[4] = {
"field" => "timeshift_enabled",
"type" => "equal",
"value" => true
}
post_data["filters"] = data_filters
post_data["from"] = 0
post_data["issuer"] = "http://github.com/tsuwatch/autots"
post_data["join"] = [
"cmsid",
"title",
"description",
"community_id",
"open_time",
"start_time",
"live_end_time",
"comment_counter",
"score_timeshift_reserved",
"provider_type",
"channel_id",
"live_status",
"tags",
"member_only"
]
post_data["order"] = "desc"
post_data["query"] = tag
post_data["reason"] = "default"
post_data["search"] = ["tags"]
post_data["service"] = ["live"]
post_data["size"] = 100
post_data["sort_by"] = "_live_recent"
post_data["timeout"] = 10000

response = Ayaneru.niconico.agent.post(URL[:search], JSON.pretty_generate(post_data))
response.body
client = Nicosa::Services::Live.new(user_agent: 'https://github.com/tsuwatch/autots')
client.search(
tag,
{
targets: 'tagsExact',
_sort: '+startTime',
filters: [
providerType: ['official'],
startTime: {
gte: Time.now.iso8601,
lt: Time.at(Time.now.to_i + until_days * 24 * 60 * 60).iso8601
}
]
}
)
end
end
end
9 changes: 3 additions & 6 deletions lib/ayaneru/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ class Server < Sinatra::Base
@registered_tags = Ayaneru.redis.lrange 'tags', 0, -1
@results = {}
@registered_tags.each do |tag|
chunks = Ayaneru.niconico.search(tag, 7).to_s.split("\n")
chunks.each do |chunk|
row = JSON.parse(chunk)
next if !(row.has_value?('hits') and row.key?('values'))
@results[tag] = row
break
@results[tag] = []
Ayaneru.niconico.search(tag, 7).each do |r|
@results[tag] << r
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/ayaneru/views/index.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
%button.btn.btn-xs.btn-danger{:type => "submit"}
%span.glyphicon.glyphicon-remove
- if @results[tag]
- @results[tag]['values'].each do |value|
- @results[tag].each do |result|
%dd
#{value['start_time']} ~ #{value['live_end_time']}
%a{:href => "http://live.nicovideo.jp/watch/#{value['cmsid']}", :target => "_blank"} #{value['title']}
#{Time.parse(result.startTime).strftime('%m月%d日 %I時%M分')} ~
%a{:href => result.url, :target => "_blank"} #{result.title}

0 comments on commit 3c9cbf7

Please sign in to comment.