|
require 'rubygems' |
|
require 'bundler/setup' |
|
require 'yaml' |
|
require 'action_view' |
|
require 'diff_match_patch_native' |
|
|
|
Bundler.require |
|
|
|
include ActionView::Helpers::TextHelper |
|
include ActionView::Helpers::DateHelper |
|
include ActionView::Helpers::NumberHelper |
|
|
|
AGENT_ID = "sob/0.0.1" |
|
|
|
@listing_template_path = "#{File.dirname(__FILE__)}/listing_template.md" |
|
@listing_template = File.open(@listing_template_path).read |
|
@config_path = __FILE__.sub(/\.rb$/, '.yml') |
|
@config = YAML.load_file(@config_path) |
|
@ob_session_options = @config[:session_options] |
|
@account = @config[:account] |
|
@chain_options = @config[:chain_options] |
|
|
|
@ob_session = OBarc::Session.new(@ob_session_options) |
|
@api = Radiator::Api.new(@chain_options) |
|
@profile = @ob_session.profile['profile'] |
|
|
|
ARGV.each do |arg| |
|
if arg =~ /contracts:.+/ |
|
@contracts = arg.split('contracts:').last.to_s.split(',') |
|
end |
|
if arg =~ /broadcast:.+/ |
|
@broadcast = arg.split('broadcast:').last.to_s.downcase == 'true' rescue false |
|
end |
|
end |
|
|
|
if @contracts.nil? || @contracts.empty? |
|
puts "Usage:" |
|
puts "\truby #{__FILE__} contracts:<guids> broadcast:true" |
|
puts "\ncontract can either be guids searated by ',' or all." |
|
exit |
|
end |
|
|
|
def merge_listing_template(contract) |
|
merged = @listing_template.dup |
|
vendor_offer = contract['vendor_offer'] |
|
listing = vendor_offer['listing'] |
|
item = listing['item'] |
|
price_per_unit = item['price_per_unit'] |
|
policy = listing['policy'] |
|
|
|
merged = merged.gsub('${store_id}', @profile['guid']) |
|
merged = merged.gsub('${contract_id}', listing['contract_id']) |
|
merged = merged.gsub('${currency_code}', price_per_unit['fiat']['currency_code']) |
|
merged = merged.gsub('${price}', number_with_precision(price_per_unit['fiat']['price'], precision: 2, delimiter: ',', separator: '.')) |
|
merged = merged.gsub('${category}', item['category']) |
|
merged = merged.gsub('${images}', images(contract)) |
|
merged = merged.gsub('${description}', description(contract)) |
|
merged = merged.gsub('${tags}', item['keywords'].join(', ')) |
|
merged = merged.gsub('${ships_to}', ships_to(contract)) |
|
merged = merged.gsub('${returns}', policy['returns']) |
|
merged = merged.gsub('${terms_conditions}', policy['terms_conditions']) |
|
|
|
merged |
|
end |
|
|
|
def description(contract) |
|
vendor_offer = contract['vendor_offer'] |
|
listing = vendor_offer['listing'] |
|
item = listing['item'] |
|
description = item['description'] |
|
|
|
description.force_encoding('BINARY').encode('UTF-8', invalid: :replace, undef: :replace, replace: ' ') |
|
end |
|
|
|
def images(contract) |
|
vendor_offer = contract['vendor_offer'] |
|
listing = vendor_offer['listing'] |
|
item = listing['item'] |
|
image_hashes = item['image_hashes'] |
|
|
|
image_hashes.map do |hash| |
|
"<img src=\"https://duosear.ch/images/#{hash}.jpg\" />\n\n" |
|
end.join |
|
end |
|
|
|
def ships_to(contract) |
|
vendor_offer = contract['vendor_offer'] |
|
listing = vendor_offer['listing'] |
|
shipping = listing['shipping'] |
|
shipping_regions = shipping['shipping_regions'] |
|
|
|
shipping_regions.map do |region| |
|
region.titleize |
|
end.join(', ') |
|
end |
|
|
|
def tags(contract) |
|
vendor_offer = contract['vendor_offer'] |
|
listing = vendor_offer['listing'] |
|
item = listing['item'] |
|
keywords = item['keywords'] |
|
['openbazaar'] + keywords[0..3] |
|
end |
|
|
|
puts "Lookup listings for: #{@profile['name']}" |
|
store_guid = @profile['guid'] |
|
|
|
@ob_session.listings['listings'].each do |listing| |
|
contract_hash = listing['contract_hash'] |
|
|
|
if @contracts == ['all'] || @contracts.include?(contract_hash) |
|
contract = @ob_session.contracts(id: contract_hash) |
|
|
|
if contract.empty? |
|
puts "Skipping contract (not found): #{contract_hash}" |
|
end |
|
|
|
response = @api.get_content(@account[:name], contract_hash) |
|
original_post = response.result |
|
|
|
body = if !!original_post.body.nil? |
|
updating = false |
|
merge_listing_template(contract) |
|
else |
|
updating = true |
|
dmp = DiffMatchPatch.new |
|
patches = dmp.patch_make original_post.body, merge_listing_template(contract) |
|
dmp.patch_to_text(patches) |
|
end |
|
|
|
if body.size == 0 |
|
puts "Skipping, nothing to post." |
|
next |
|
end |
|
|
|
metadata = { |
|
tags: tags(contract), |
|
app: AGENT_ID |
|
} |
|
|
|
post = { |
|
type: :comment, |
|
parent_permlink: tags(contract).first, |
|
author: @account[:name], |
|
title: listing['title'], |
|
permlink: contract_hash, |
|
body: body, |
|
json_metadata: metadata.to_json, |
|
parent_author: '' |
|
} |
|
|
|
slug = "@#{post[:author]}/#{post[:permlink]}" |
|
tx = Radiator::Transaction.new(@chain_options.merge(wif: @account[:posting_wif])) |
|
op = Radiator::Operation.new(post) |
|
tx.operations << op |
|
response = tx.process(@broadcast) |
|
|
|
if defined?(response.error) && !!response.error |
|
puts response.error.message |
|
exit |
|
end |
|
|
|
if defined?(response.result) |
|
if updating |
|
print "Updated Listing: " |
|
else |
|
print "Created Listing: " |
|
end |
|
|
|
puts "#{response.result.to_json}\n\thttps://steemit.com/#{post[:parent_permlink]}/#{slug}" |
|
else |
|
puts "Dry Run: to post contract, use: ruby #{__FILE__} @contracts:#{contract_hash} broadcast:true\n\thttps://steemit.com/openbazaar/@#{@account[:name]}/#{contract_hash}" |
|
end |
|
end |
|
end |