<commit_before>require 'json'
require 'net/http'
require 'uri'
require 'htmlentities'

class Tweet < RubotPlugin
	def initialize
		@accounts = {:wikileaks => 0, :wired => 0}
		@index = 0
		@needs_to_be_said = []
		
		@coder = HTMLEntities.new
		
		@h = {
			"User-Agent" => "Mozilla/5.0 (Ruby-Twitter; +https://github.com/savetheinternet/Rubot-Twitter)",
			"Accept-Language" => "en-US,en;q=0.8",
			"Referer" => "https://github.com/savetheinternet/Rubot-Twitter"
		}
		
		Thread.new {
			loop {
				do_tweets
				sleep 5
			}
		}
		
		Thread.new {
			loop {
				@needs_to_be_said.each do |line|
					@client.channels.keys.each { |channel| say channel, line }
					@needs_to_be_said.delete line
					sleep 0.3
				end
				sleep 1
			}
		}
	end
	
	def do_tweets
		username = @accounts.keys[@index]
		@accounts[username] = check_new_tweets(username.to_s, @accounts[username])
		
		@index+=1
		if @index >= @accounts.count then
			@index = 0
		end
	end
	
	def announce_tweets(tweets, channel=nil)
		tweets.each do |tweet|
			from = @coder.decode(tweet['from_user'])
			message = @coder.decode(tweet['text'].split("\n").join " ")
			
			if channel.nil? then
				@needs_to_be_said.push "\002[#{from}]\002 #{message}"
			else
				say channel, "\002[#{from}]\002 #{message}"
			end
		end
	end
	
	def search_tweets(channel, query, type)
		uri = "/search.json?lang=en&q=#{query}&rpp=5&result_type=#{type}"
		
		Net::HTTP.new("search.twitter.com", 80).start do |http|
			res = http.get uri, @h
			timeline = JSON.parse res.body
			if not timeline.include? 'results' then
				return "\002Error\002: [#{uri}] #{timeline.to_s}"
			end
			
			if timeline['results'].empty? then
				return "No results."
			end
			announce_tweets timeline['results'].reverse, channel
		end
		nil
	end
	
	def format_number(n)
		return n.to_s.reverse.gsub(/...(?=.)/, '\&,').reverse
	end
	
	def do_info(channel, username)
		uri = "/1/users/show/#{username}.json"
		Net::HTTP.new("api.twitter.com", 80).start do |http|
			res = http.get uri, @h
			info = JSON.parse res.body
			
			@info = [
				["Name", "name"],
				["Description", "description"],
				["URL", "url"],
				["Created", "created_at"],
				["Followers", "followers_count"]
			]
			
			if info.include? "error" then
				say channel, "\002Error\002: #{info["error"]}"
				return
			end
			
			@info.each do |x|
				name = x[0]
				key = x[1]
				
				if info.include? key then
					if info[key].class == Fixnum then
						info[key] = format_number info[key]
					end
					if !info[key].nil? && !info[key].empty? then
						say channel, "\002#{name}\002: #{info[key]}"
					end
				end
			end
		end
	end
	
	def do_latest(channel, username, count)
		uri = "/search.json?lang=en&from=#{username}&rpp=#{count}"
		Net::HTTP.new("search.twitter.com", 80).start do |http|
			res = http.get uri, @h
			
			timeline = JSON.parse res.body
			
			# Show tweets in reverse order. Oldest first.
			if not timeline.include? 'results' then
				@needs_to_be_said.push "\002Error\002: [#{uri}] #{timeline.to_s}"
				return since_id
			end
			
			if timeline['results'].empty? then
				return "No tweets."
			end
			tweets = timeline['results'].reverse
			
			announce_tweets tweets, channel
		end
	end<commit_msg>Multi-line info<commit_after>require 'json'
require 'net/http'
require 'uri'
require 'htmlentities'

class Tweet < RubotPlugin
	def initialize
		@accounts = {:wikileaks => 0, :wired => 0}
		@index = 0
		@needs_to_be_said = []
		
		@coder = HTMLEntities.new
		
		@h = {
			"User-Agent" => "Mozilla/5.0 (Ruby-Twitter; +https://github.com/savetheinternet/Rubot-Twitter)",
			"Accept-Language" => "en-US,en;q=0.8",
			"Referer" => "https://github.com/savetheinternet/Rubot-Twitter"
		}
		
		Thread.new {
			loop {
				do_tweets
				sleep 5
			}
		}
		
		Thread.new {
			loop {
				@needs_to_be_said.each do |line|
					@client.channels.keys.each { |channel| say channel, line }
					@needs_to_be_said.delete line
					sleep 0.3
				end
				sleep 1
			}
		}
	end
	
	def do_tweets
		username = @accounts.keys[@index]
		@accounts[username] = check_new_tweets(username.to_s, @accounts[username])
		
		@index+=1
		if @index >= @accounts.count then
			@index = 0
		end
	end
	
	def announce_tweets(tweets, channel=nil)
		tweets.each do |tweet|
			from = @coder.decode(tweet['from_user'])
			message = @coder.decode(tweet['text'].split("\n").join " ")
			
			if channel.nil? then
				@needs_to_be_said.push "\002[#{from}]\002 #{message}"
			else
				say channel, "\002[#{from}]\002 #{message}"
			end
		end
	end
	
	def search_tweets(channel, query, type)
		uri = "/search.json?lang=en&q=#{query}&rpp=5&result_type=#{type}"
		
		Net::HTTP.new("search.twitter.com", 80).start do |http|
			res = http.get uri, @h
			timeline = JSON.parse res.body
			if not timeline.include? 'results' then
				return "\002Error\002: [#{uri}] #{timeline.to_s}"
			end
			
			if timeline['results'].empty? then
				return "No results."
			end
			announce_tweets timeline['results'].reverse, channel
		end
		nil
	end
	
	def format_number(n)
		return n.to_s.reverse.gsub(/...(?=.)/, '\&,').reverse
	end
	
	def do_info(channel, username)
		uri = "/1/users/show/#{username}.json"
		Net::HTTP.new("api.twitter.com", 80).start do |http|
			res = http.get uri, @h
			info = JSON.parse res.body
			
			@info = [
				["Screen name", "screen_name"],
				["Name", "name"],
				["Location", "location"],
				["Description", "description"],
				["URL", "url"],
				["Created", "created_at"],
				["Followers", "followers_count"]
			]
			
			if info.include? "error" then
				say channel, "\002Error\002: #{info["error"]}"
				return
			end
			
			@info.each do |x|
				name = x[0]
				key = x[1]
				
				if info.include? key then
					if info[key].class == Fixnum then
						info[key] = format_number info[key]
					end
					if !info[key].nil? && !info[key].empty? then
						say channel, "\002#{name}\002: #{info[key].split("\n").join " "}"
					end
				end
			end
		end
	end
	
	def do_latest(channel, username, count)
		uri = "/search.json?lang=en&from=#{username}&rpp=#{count}"
		Net::HTTP.new("search.twitter.com", 80).start do |http|
			res = http.get uri, @h
			
			timeline = JSON.parse res.body
			
			# Show tweets in reverse order. Oldest first.
			if not timeline.include? 'results' then
				@needs_to_be_said.push "\002Error\002: [#{uri}] #{timeline.to_s}"
				return since_id
			end
			
			if timeline['results'].empty? then
				return "No tweets."
			end
			tweets = timeline['results'].reverse
			
			announce_tweets tweets, channel
		end
	end
{'fraction_non_alphanumeric': 0.16398959767477436, 'fraction_numerical': 0.0180510937739024, 'mean_word_length': 2.9648271679805944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, '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': '1508861', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2395, 'n_words': 525}