ruby on rails - Trouble getting the first 25 values that are not present in another array -
hey know do
(array - array 2).first(25)
and first 25 values not present when comparing 2 arrays, situation bit more difficult.
i have twitter app store search queries. run background job every hour, , if query not popular might save same query. want first 25. example end getting 18, because have model validation tweet id must unique user.
what know twitter search gives 100 recent tweets of result. in order combat situation map current tweet ids have, , use on query, , first 25 of results.
the problem need store more info tweet id, if not have been easy code shown. here have currently:
def save_keyword active_campaigns = campaigns.where(live: true) total_keywords = active_campaigns.count.to_i tweet_amount = (get_tweets_per_hour / total_keywords).to_f.floor rescue 0 begin active_campaigns.each |campaign| self.twitter.search(campaign.keyword, result_type: 'recent').take(tweet_amount).map |tweet| favorites.create |favorite| favorite.tweet_id = tweet.id favorite.owner_id = tweet.user.id favorite.campaign_id = campaign.id end end end rescue twitter::error::toomanyrequests => error sleep error.rate_limit.reset_in + 1 retry rescue twitter::error::unauthorized => error logger.error 'unauthorized access' rescue => error logger.error 'save_keyword error' end end
as said store info owner_id, tweet_id , campaign id. need create code gives me, or keeps on saving keyword until it's 25.
you querying favorite
, using tweet id filter results:
old_tweets = favorite.where( campaign_id: campaign.id ).pluck(:tweet_id) tweets = self.twitter.search(campaign.keyword, result_type: 'recent') new_tweets = tweets.reject { |t| old_tweets.include?(t.id) }.take(tweet_amount)
Comments
Post a Comment