<commit_before>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

class TwitterUserPoller
  REFRESH_INTERVAL = 30.minutes
  REFRESH_INTERVAL_EMPTY = 60.minutes
  MAX_PER_PROCESS = 100
  
  include Twitter
  
  def self.process
    TwitterUserPoller.new.process
  end
  
  def initialize
    @logger = RAILS_DEFAULT_LOGGER
  end
  
  def retrieve_tweets(service, attempt=0)
    @twitter_service = service
    twitter_list(nil, service.last_result_id)
  end
  
  def process
    count = 0
    
    while service = UserService.to_be_polled.for_service('twitter').first and (count += 1) < MAX_PER_PROCESS
      service.updated_at => Time.now.utc
      service.save
      
      @logger.info("user found: #{service.service_user_name} retrieving tweets...")
      since_id = nil
      count = 0
      tweets = nil
      begin
        tweets = retrieve_tweets(service)
      rescue => e
        retry_after = REFRESH_INTERVAL_EMPTY
        if e.to_s =~ /Retry After (\d+)/
          retry_after = $1
          @logger.info("throttled!  Retry after: #{retry_after}")
        else
          ErrorReport.log_exception(:processing, e, {
            :message => e.to_s,
            :url => (request.url rescue "none")
          })
          @logger.info("unexpected error: #{e.to_s} #{e.backtrace.join "\n"}")
        end
        retry_after = [REFRESH_INTERVAL_EMPTY, retry_after].max
        service.refresh_at Time.now.utc + retry_after + 1.minute
        service.save
      end
      
      if tweets
        @logger.info("found #{tweets.length} tweets")
        tweets.each do |tweet|
          scans = (tweet['text'] || '').scan(/#([^#\s])/)
          @logger.info("message found: #{tweet['id']} with #{scans.length} hashtags")
          scans.each do |scan|
            hash = scan[0]
            if hashtag = Hashtag.find_by_hashtag(hash)
              hashtag.add_short_message(service.user, tweet, false)
              @logger.info("added for #{hash}")
            end
          end
          since_id ||= tweet['id'] if tweet['id']
        end
        service.last_result_id = since_id if since_id
        service.refresh_at = Time.now.utc + (since_id ? REFRESH_INTERVAL : REFRESH_INTERVAL_EMPTY)
        service.save
      end
    end
    
    if count >= MAX_PER_PROCESS
      @logger.info("more services to process... scheduling another job")
      TwitterUserPoller.send_later_engueue_args(:process, { :priority => Delayed::LOW_PRIORITY })
    end
  end
end
<commit_msg>fix syntax errors in twitter_user_poller<commit_after>#
# Copyright (C) 2011 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

class TwitterUserPoller
  REFRESH_INTERVAL = 30.minutes
  REFRESH_INTERVAL_EMPTY = 60.minutes
  MAX_PER_PROCESS = 100
  
  include Twitter
  
  def self.process
    TwitterUserPoller.new.process
  end
  
  def initialize
    @logger = RAILS_DEFAULT_LOGGER
  end
  
  def retrieve_tweets(service, attempt=0)
    @twitter_service = service
    twitter_list(nil, service.last_result_id)
  end
  
  def process
    count = 0
    
    while service = UserService.to_be_polled.for_service('twitter').first and (count += 1) < MAX_PER_PROCESS
      service.updated_at = Time.now.utc
      service.save
      
      @logger.info("user found: #{service.service_user_name} retrieving tweets...")
      since_id = nil
      count = 0
      tweets = nil
      begin
        tweets = retrieve_tweets(service)
      rescue => e
        retry_after = REFRESH_INTERVAL_EMPTY
        if e.to_s =~ /Retry After (\d+)/
          retry_after = $1
          @logger.info("throttled!  Retry after: #{retry_after}")
        else
          ErrorReport.log_exception(:processing, e, {
            :message => e.to_s,
            :url => (request.url rescue "none")
          })
          @logger.info("unexpected error: #{e.to_s} #{e.backtrace.join "\n"}")
        end
        retry_after = [REFRESH_INTERVAL_EMPTY, retry_after].max
        service.refresh_at = Time.now.utc + retry_after + 1.minute
        service.save
      end
      
      if tweets
        @logger.info("found #{tweets.length} tweets")
        tweets.each do |tweet|
          scans = (tweet['text'] || '').scan(/#([^#\s])/)
          @logger.info("message found: #{tweet['id']} with #{scans.length} hashtags")
          scans.each do |scan|
            hash = scan[0]
            if hashtag = Hashtag.find_by_hashtag(hash)
              hashtag.add_short_message(service.user, tweet, false)
              @logger.info("added for #{hash}")
            end
          end
          since_id ||= tweet['id'] if tweet['id']
        end
        service.last_result_id = since_id if since_id
        service.refresh_at = Time.now.utc + (since_id ? REFRESH_INTERVAL : REFRESH_INTERVAL_EMPTY)
        service.save
      end
    end
    
    if count >= MAX_PER_PROCESS
      @logger.info("more services to process... scheduling another job")
      TwitterUserPoller.send_later_engueue_args(:process, { :priority => Delayed::LOW_PRIORITY })
    end
  end
end

{'fraction_non_alphanumeric': 0.0990990990990991, 'fraction_numerical': 0.006113256113256113, 'mean_word_length': 2.54851598173516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6784332', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1932, 'n_words': 532}