-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from true-runes/development
v3.1.0
- Loading branch information
Showing
5 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class SubGensosenkyoFav < ApplicationRecord | ||
belongs_to :tweet, optional: true | ||
|
||
def url_as_screen_name_is_twitter | ||
"https://twitter.com/twitter/status/#{id_number}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# rubocop:disable Rails/Output | ||
module Tweets | ||
class RetrieveFavorites | ||
MAX_LOOP_COUNT = 20 | ||
INIT_NEXT_MAX_ID = 1 | ||
|
||
def self.all(client = nil) | ||
client = TwitterRestApi.client(account_key: :sub_gensosenkyo) if client.nil? | ||
|
||
all_favorite_tweets = [] | ||
api_request_base_options = { count: 200 } | ||
next_max_id = INIT_NEXT_MAX_ID | ||
|
||
# max_id は「その数以下」を示す(その数自身を含む) | ||
MAX_LOOP_COUNT.times do |i| | ||
options = api_request_base_options | ||
options.merge!({ max_id: next_max_id }) if next_max_id != INIT_NEXT_MAX_ID | ||
|
||
# API が消費される | ||
fav_tweets = client.favorites(options) # ツイートID 降順 の Array | ||
all_favorite_tweets << fav_tweets | ||
all_favorite_tweets.flatten! | ||
|
||
puts "[LOG] #{i + 1}回目 のループでの fav_tweets を取得しました。" | ||
puts "[LOG] #{fav_tweets.count}件 の fav_tweets を取得しました。" | ||
|
||
break if fav_tweets.empty? | ||
|
||
next_max_id = fav_tweets.last.id - 1 | ||
puts "[LOG] next_max_id は #{next_max_id} です。" | ||
|
||
sleep 5 | ||
end | ||
|
||
save_fav_tweets(all_favorite_tweets) | ||
end | ||
|
||
def self.continuous(client = nil) | ||
return 'SubGensosenkyoFav にレコードが存在しません' if SubGensosenkyoFav.first.nil? | ||
|
||
client = TwitterRestApi.client(account_key: :sub_gensosenkyo) if client.nil? | ||
|
||
all_favorite_tweets = [] | ||
api_request_base_options = { count: 200 } | ||
next_since_id = SubGensosenkyoFav.order(id_number: :desc).first.id_number | ||
|
||
# since_id は「その数より大きい」を示す(その数自身は含まない) | ||
MAX_LOOP_COUNT.times do |i| | ||
options = api_request_base_options | ||
options.merge!({ since_id: next_since_id }) | ||
|
||
# API が消費される | ||
fav_tweets = client.favorites(options) # ツイートID 降順 の Array | ||
all_favorite_tweets << fav_tweets | ||
all_favorite_tweets.flatten! | ||
|
||
puts "[LOG] #{i + 1}回目 のループでの fav_tweets を取得しました。" | ||
puts "[LOG] #{fav_tweets.count}件 の fav_tweets を取得しました。" | ||
|
||
break if fav_tweets.empty? | ||
|
||
next_since_id = fav_tweets.first.id | ||
puts "[LOG] next_since_id は #{next_since_id} です。" | ||
|
||
sleep 5 | ||
end | ||
|
||
save_fav_tweets(all_favorite_tweets) | ||
end | ||
|
||
def self.save_fav_tweets(fav_tweets, model_name: 'SubGensosenkyoFav') | ||
return if model_name != 'SubGensosenkyoFav' | ||
|
||
ActiveRecord::Base.transaction do | ||
fav_tweets.each do |fav_tweet| | ||
next if SubGensosenkyoFav.exists?(id_number: fav_tweet.id) | ||
|
||
sub_gensosenkyo_fav = SubGensosenkyoFav.new( | ||
id_number: fav_tweet.id, | ||
tweet: Tweet.find_by(id_number: fav_tweet.id) | ||
) | ||
|
||
sub_gensosenkyo_fav.save! | ||
end | ||
end | ||
end | ||
end | ||
end | ||
# rubocop:enable Rails/Output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateSubGensosenkyoFavs < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :sub_gensosenkyo_favs do |t| | ||
t.bigint :id_number, null: false | ||
t.references :tweet | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace :save_favorites do | ||
desc 'リアルタイムレポートのデータベースのデータを更新する' | ||
task continuous: :environment do | ||
Tweets::RetrieveFavorites.continuous | ||
|
||
'[DONE] ふぁぼ の継続保存が完了しました' | ||
end | ||
end |