Skip to content

Instantly share code, notes, and snippets.

@jsn
Created July 9, 2017 20:25
Show Gist options
  • Save jsn/97d0c839aac23c57a1abcc590dd1f629 to your computer and use it in GitHub Desktop.
Save jsn/97d0c839aac23c57a1abcc590dd1f629 to your computer and use it in GitHub Desktop.
Mokum reciprocative autosubscriber
#! /usr/bin/env ruby
require 'uri'
require 'net/http'
require 'json'
class Mukom < Net::HTTP
BASE = URI.parse 'https://mokum.place/api/v1/'
attr_accessor :api_key
class << self
def init api_key
this = Mukom.new BASE.host, BASE.port
this.use_ssl = true
this.api_key = api_key
this
end
end
def run &block
start do
instance_eval &block
end
end
def headers
{
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-API-Token' => api_key
}
end
def get str
rep = request Net::HTTP::Get.new(URI.join(BASE, "#{str}.json"), headers)
raise 'oops' unless Net::HTTPOK === rep
JSON.parse rep.body
end
def post str, data = ''
rep = request Net::HTTP::Post.new(URI.join(BASE, "#{str}.json"), headers)
raise 'oops' unless Net::HTTPOK === rep
JSON.parse rep.body
end
end
api_key = ARGV.shift or raise 'token must be specified'
Mukom.init(api_key).run do
me = get(:whoami)['user']['name']
puts "whoami: #{me}"
get("/#{me}/subscribers")['subscribers'].each do |s|
u = s['user'] or next
if s['mutual']
puts "# #{u['name']} (mutual)"
next
end
puts "+ #{u['name']} (#{post("users/#{u['name']}/subscribers")['status']})"
sleep 2 # chill, dude
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment