started to work on the command line application
added -- raw -- scrape_public_pastes -- list_trending (still broken though. need to fix the api call) -- added a method to extract just the keys from public pastes -- moved the previous tests to an examples.rb file need to mess around with all the different ways to parse arguments for the command line program.
This commit is contained in:
parent
092bbb252b
commit
eae20ccd0c
3 changed files with 85 additions and 27 deletions
|
@ -1,2 +1,50 @@
|
||||||
#!/usr/bin/env ruby
|
#!/usr/bin/env ruby
|
||||||
require 'pastebinner'
|
require '../lib/pastebinner'
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
|
# setup our object and grab a session key
|
||||||
|
pb = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
|
||||||
|
api_dev_key = ENV['pastebin_api_key']
|
||||||
|
|
||||||
|
opts = {}
|
||||||
|
|
||||||
|
OptionParser.new do |parser|
|
||||||
|
parser.banner = "Usage: pastebinner [options]"
|
||||||
|
|
||||||
|
parser.on('-h', '--help', "Show this help message") do ||
|
||||||
|
puts parser
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
parser.on('-sp', '--scrape_public', "Scrape public pastes") do |s|
|
||||||
|
opts[:sp] = true
|
||||||
|
end
|
||||||
|
parser.on('-r', '--raw', "Raw paste. Requires --key passed with a valid key") do |r|
|
||||||
|
opts[:r] = true
|
||||||
|
end
|
||||||
|
parser.on('-k', '--key=', "Unique paste key") do |k|
|
||||||
|
opts[:k] = k
|
||||||
|
end
|
||||||
|
parser.on('-gk', '--get_keys', "Get unique paste keys from public pastes") do |gk|
|
||||||
|
opts[:gk] = true
|
||||||
|
end
|
||||||
|
parser.on('-t', '--trending', 'Trending pastes.') do |t|
|
||||||
|
opts[:t] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
end.parse!
|
||||||
|
|
||||||
|
#if opts[:sp]
|
||||||
|
# puts pb.scrape_public_pastes
|
||||||
|
#elsif opts[:r] and opts[:k]
|
||||||
|
# key = opts[:k]
|
||||||
|
# puts pb.raw_paste_data(key)
|
||||||
|
#elsif opts[:t]
|
||||||
|
# puts pb.list_trending_pastes
|
||||||
|
#elsif opts[:gk]
|
||||||
|
# puts pb.get_unique_paste_key
|
||||||
|
#else
|
||||||
|
# puts 'please provide arguments'
|
||||||
|
#end
|
||||||
|
|
||||||
|
#pp = pb.scrape_public_pastes
|
||||||
|
#puts pb.get_unique_paste_keys(pp)
|
||||||
|
|
29
lib/examples.rb
Executable file
29
lib/examples.rb
Executable file
|
@ -0,0 +1,29 @@
|
||||||
|
#!/usr/bin/env ruby
|
||||||
|
|
||||||
|
require './pastebinner'
|
||||||
|
|
||||||
|
######################## TESTING ####################################################
|
||||||
|
#####################################################################################
|
||||||
|
|
||||||
|
#### INITIAL STEPS
|
||||||
|
|
||||||
|
# setup our object and grab a session key
|
||||||
|
pb = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
|
||||||
|
api_dev_key = ENV['pastebin_api_key']
|
||||||
|
|
||||||
|
#### CREATE PASTE
|
||||||
|
# prepare some sample paste data to send
|
||||||
|
#paste_data = 'this is a test paste two two two.'
|
||||||
|
# prepare our paste params
|
||||||
|
#params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data }
|
||||||
|
#puts pb.create_paste(params)
|
||||||
|
|
||||||
|
#### SCRAPE PUBLIC PASTES
|
||||||
|
#puts pb.scrape_public_pastes
|
||||||
|
|
||||||
|
#### SCRAPING - WHITELISTED IP ONLY
|
||||||
|
#### SCRAPE RAW PASTE DATA OF A PASTE KEY
|
||||||
|
#puts pb.raw_paste_data('Gkb4ukK9')
|
||||||
|
|
||||||
|
#### SCRAPE RAW METADATA OF A PASTE KEY (WORKS WITH WHITELISTED IP ONLY)
|
||||||
|
#puts pb.raw_paste_metadata('Gkb4ukK9')
|
|
@ -3,6 +3,7 @@
|
||||||
# a ruby wrapper around all of the methods pastebin provides with its api
|
# a ruby wrapper around all of the methods pastebin provides with its api
|
||||||
# official docs from pastebin on their api can be found at https://pastebin.com/api
|
# official docs from pastebin on their api can be found at https://pastebin.com/api
|
||||||
require 'rest-client'
|
require 'rest-client'
|
||||||
|
require 'json'
|
||||||
|
|
||||||
class Pastebinner
|
class Pastebinner
|
||||||
attr_accessor :api_dev_key, :username, :password
|
attr_accessor :api_dev_key, :username, :password
|
||||||
|
@ -107,6 +108,12 @@ class Pastebinner
|
||||||
url: @scraping_api_url + ENDPOINTS[:scraping])
|
url: @scraping_api_url + ENDPOINTS[:scraping])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# will extract just the keys from recent public pastes
|
||||||
|
def get_unique_paste_keys(public_pastes)
|
||||||
|
pp = JSON.parse(public_pastes)
|
||||||
|
pp.map {|p| {'key': p['key']}}
|
||||||
|
end
|
||||||
|
|
||||||
def raw_paste_data(unique_paste_key)
|
def raw_paste_data(unique_paste_key)
|
||||||
response = RestClient::Request.execute(
|
response = RestClient::Request.execute(
|
||||||
method: :get,
|
method: :get,
|
||||||
|
@ -132,29 +139,3 @@ class Pastebinner
|
||||||
# make my own exception class
|
# make my own exception class
|
||||||
# inherit ruby standard error class
|
# inherit ruby standard error class
|
||||||
end
|
end
|
||||||
|
|
||||||
######################## TESTING ####################################################
|
|
||||||
#####################################################################################
|
|
||||||
|
|
||||||
#### INITIAL STEPS
|
|
||||||
|
|
||||||
# setup our object and grab a session key
|
|
||||||
pb = Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
|
|
||||||
api_dev_key = ENV['pastebin_api_key']
|
|
||||||
|
|
||||||
#### CREATE PASTE
|
|
||||||
# prepare some sample paste data to send
|
|
||||||
#paste_data = 'this is a test paste two two two.'
|
|
||||||
# prepare our paste params
|
|
||||||
#params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data }
|
|
||||||
#puts pb.create_paste(params)
|
|
||||||
|
|
||||||
#### SCRAPE PUBLIC PASTES
|
|
||||||
puts pb.scrape_public_pastes
|
|
||||||
|
|
||||||
#### SCRAPING - WHITELISTED IP ONLY
|
|
||||||
#### SCRAPE RAW PASTE DATA OF A PASTE KEY
|
|
||||||
#puts pb.raw_paste_data('Gkb4ukK9')
|
|
||||||
|
|
||||||
#### SCRAPE RAW METADATA OF A PASTE KEY (WORKS WITH WHITELISTED IP ONLY)
|
|
||||||
#puts pb.raw_paste_metadata('Gkb4ukK9')
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue