<commit_before>#!/usr/bin/env ruby

require 'dotenv'
require 'curb'
require 'digest'
require 'json'
require 'pp'

Dotenv.load

class Comic
  attr_reader :uri, :issue, :title, :year, :id

  def initialize h
    regex   = /(.*) \(([0-9][0-9][0-9][0-9])\).*/
    matches = regex.match h['title']
    @title  = matches[1]
    @year   = matches[2]
    @issue  = h['issueNumber']
    @id     = h['id']

    @uri = h['resourceURI']
  end

  def to_s
    "%s issue %s, %s" % [
        @title,
        @issue,
        @year
    ]
  end
end

def host
  'http://gateway.marvel.com'
end

def prefix
  'v1/public'
end

def params
  ({
      'limit'     => 50,
      'dateRange' => '1983-01-01,1990-01-01'
  }.map { |k, v| k + '=' + URI.encode(v.to_s) }).join '&'
end

def id
  begin
    (File.read 'last.id').strip
  rescue
    '1009351' # Hulk
  end
end

def write_id id
  File.open 'last.id', 'w' do |f|
    f.write id
  end
end

def auth
  timestamp = Time.new.strftime '%s'

  digest = Digest::MD5.hexdigest '%s%s%s' % [
      timestamp,
      ENV['PRIVATE_KEY'],
      ENV['PUBLIC_KEY']
  ]

  'ts=%s&apikey=%s&hash=%s' % [
      timestamp,
      ENV['PUBLIC_KEY'],
      digest
  ]
end

def fetch url
#  require 'pry'
#  binding.pry

  c         = Curl::Easy.new("%s" % url)
  c.headers = {
      'Accept' => 'application/json'
  }
  c.perform

  h = JSON.parse c.body_str
  h['data']['results']
end

def url path, with_date = true
  params = 'limit=100'
  if with_date
    params = '%s&dateRange=1960-01-01%%2C1990-01-01' % params
  end

  u = '%s/%s/%s?%s&%s' % [
      host,
      prefix,
      path,
      params,
      auth
  ]
end

character = (fetch url 'characters/%s' % [id])[0]

comics = fetch url 'characters/%s/comics' % [id]

comic = Comic.new comics.sample

characters     = fetch url 'comics/%s/characters' % [comic.id], with_date = false
next_character = characters.sample

write_id next_character['id']

puts "%s appeared in %s #%s with %s" % [
    character['name'],
    comic.title,
    comic.issue,
    next_character['name']
]
<commit_msg>Whevs<commit_after>#!/usr/bin/env ruby

require 'dotenv'
require 'curb'
require 'digest'
require 'json'
require 'pp'

Dotenv.load

class Comic
  attr_reader :uri, :issue, :title, :year, :id

  def initialize h
    regex   = /(.*) \(([0-9][0-9][0-9][0-9])\).*/
    matches = regex.match h['title']
    @title  = matches[1]
    @year   = matches[2]
    @issue  = h['issueNumber']
    @id     = h['id']

    @uri = h['resourceURI']
  end

  def to_s
    "%s issue %s, %s" % [
        @title,
        @issue,
        @year
    ]
  end
end

def host
  'http://gateway.marvel.com'
end

def prefix
  'v1/public'
end

def params
  ({
      'limit'     => 50,
      'dateRange' => '1983-01-01,1990-01-01'
  }.map { |k, v| k + '=' + URI.encode(v.to_s) }).join '&'
end

def id
  begin
    (File.read 'last.id').strip
  rescue
    '1009351' # Hulk
  end
end

def write_id id
  File.open 'last.id', 'w' do |f|
    f.write id
  end
end

def auth
  timestamp = Time.new.strftime '%s'

  digest = Digest::MD5.hexdigest '%s%s%s' % [
      timestamp,
      ENV['PRIVATE_KEY'],
      ENV['PUBLIC_KEY']
  ]

  'ts=%s&apikey=%s&hash=%s' % [
      timestamp,
      ENV['PUBLIC_KEY'],
      digest
  ]
end

def fetch url
#  require 'pry'
#  binding.pry

  c         = Curl::Easy.new("%s" % url)
  c.headers = {
      'Accept' => 'application/json'
  }
  c.perform

  h = JSON.parse c.body_str
  h['data']['results']
end

def url path, with_date = true
  params = 'limit=100'
  if with_date
    params = '%s&dateRange=1960-01-01%%2C1990-01-01' % params
  end

  u = '%s/%s/%s?%s&%s' % [
      host,
      prefix,
      path,
      params,
      auth
  ]
  puts u
  u
end

character = (fetch url 'characters/%s' % [id])[0]

comics = fetch url 'characters/%s/comics' % [id]

comic = Comic.new comics.sample

characters     = fetch url 'comics/%s/characters' % [comic.id], with_date = false
next_character = characters.sample

write_id next_character['id']

puts ''
puts "%s appeared in %s #%s with %s" % [
    character['name'],
    comic.title,
    comic.issue,
    next_character['name']
]

{'fraction_non_alphanumeric': 0.1743141539208546, 'fraction_numerical': 0.028162175285263415, 'mean_word_length': 2.4505862646566166, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '285730', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1659, 'n_words': 374}