#!/usr/bin/env ruby require 'twitter_ebooks' $debug = true module Ebooks APP_PATH = Dir.pwd # XXX do some recursive thing instead def self.new(reponame) usage = "Usage: ebooks new " if reponame.nil? log usage exit end reponame = "./#{reponame}" if File.exists?(reponame) log "#{reponame} already exists. Please remove if you want to recreate." exit end FileUtils.cp_r(SKELETON_PATH, reponame) File.open(File.join(reponame, 'bots.rb'), 'w') do |f| template = File.read(File.join(SKELETON_PATH, 'bots.rb')) f.write(template.gsub("{{BOT_NAME}}", reponame)) end log "New twitter_ebooks app created at #{reponame}" end def self.consume(pathes) pathes.each do |path| filename = File.basename(path) shortname = filename.split('.')[0..-2].join('.') hash = Digest::MD5.hexdigest(File.read(path)) outpath = File.join(APP_PATH, 'model', "#{shortname}.model") Model.consume(path).save(outpath) log "Corpus consumed to #{outpath}" end end def self.gen(model_path, input) model = Model.load(model_path) if input && !input.empty? puts "@cmd " + model.make_response(input, 135) else puts model.make_statement end end def self.score(model_path, input) model = Model.load(model_path) model.score_interest(input) end def self.archive(username, outpath) Archive.new(username, outpath).sync end def self.tweet(modelpath, username) load File.join(APP_PATH, 'bots.rb') model = Model.load(modelpath) statement = model.make_statement log "@#{username}: #{statement}" bot = Bot.get(username) bot.configure bot.tweet(statement) end def self.jsonify(paths) paths.each do |path| name = File.basename(path).split('.')[0] new_path = name + ".json" tweets = [] id = nil File.read(path).split("\n").each do |l| if l.start_with?('# ') id = l.split('# ')[-1] else tweet = { text: l } if id tweet[:id] = id id = nil end tweets << tweet end end File.open(new_path, 'w') do |f| log "Writing #{tweets.length} tweets to #{new_path}" f.write(JSON.pretty_generate(tweets)) end end end def self.command(args) usage = """Usage: ebooks new ebooks consume [...] ebooks gen [input] ebooks score ebooks archive <@user> ebooks tweet <@bot> ebooks jsonify [...] """ if args.length == 0 log usage exit end case args[0] when "new" then new(args[1]) when "consume" then consume(args[1..-1]) when "gen" then gen(args[1], args[2..-1].join(' ')) when "score" then score(args[1], args[2..-1].join(' ')) when "archive" then archive(args[1], args[2]) when "tweet" then tweet(args[1], args[2]) when "jsonify" then jsonify(args[1..-1]) end end end Ebooks.command(ARGV)