2018-08-16 14:54:08 -05:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
# author: brendan mcdevitt
|
|
|
|
# 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
|
|
|
|
require 'rest-client'
|
2018-08-16 20:31:34 -05:00
|
|
|
|
|
|
|
module Pastebin
|
|
|
|
class Pastebinner
|
|
|
|
attr_reader :api_user_key
|
2018-08-16 14:54:08 -05:00
|
|
|
|
2018-08-16 20:31:34 -05:00
|
|
|
def initialize(api_dev_key, username, password)
|
2018-08-16 14:54:08 -05:00
|
|
|
@api_dev_key = api_dev_key
|
2018-08-16 20:31:34 -05:00
|
|
|
@username = username
|
|
|
|
@password = password
|
2018-08-16 14:54:08 -05:00
|
|
|
@base_api_url = 'https://pastebin.com/api'
|
|
|
|
@scraping_api_url = 'https://scrape.pastebin.com'
|
2018-08-16 20:31:34 -05:00
|
|
|
@api_user_key = self.get_api_user_key
|
2018-08-16 14:54:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# this should be a hash of { endpoint_name: '/url_endpoint.php'}
|
|
|
|
ENDPOINTS = { :login => '/api_login.php',
|
|
|
|
:post => '/api_post.php',
|
|
|
|
:raw => '/api_raw.php',
|
|
|
|
:scraping => '/api_scraping.php',
|
|
|
|
:scrape_item => '/api_scrape_item.php',
|
|
|
|
:srape_item_meta => '/api_scrape_item_meta.php' }
|
|
|
|
|
|
|
|
# basic example hash for creating a paste:
|
|
|
|
# params = { 'api_dev_key': @api_dev_key, 'api_option': 'paste'. 'api_paste_code': paste_data}
|
|
|
|
|
|
|
|
# required params:
|
|
|
|
# api_dev_key - your unique developer api key
|
|
|
|
# api_option - set as paste, this will indicate you want to create a new paste
|
|
|
|
# api_paste_code - this is the text that will be written inside of your paste
|
|
|
|
|
|
|
|
# optional params:
|
|
|
|
# api_user_key - this parameter is part of the login system, which is explained further down the page
|
|
|
|
# api_paste_name - this will be the name / title of your paste
|
|
|
|
# api_paste_format - this will be the syntax highlighting value, which is explained in detail further down the page
|
|
|
|
# api_paste_private - this makes a paste public, unlisted, or private, public = 0, unlisted = 1, private = 2
|
|
|
|
# api_paste_expire_date - this sets the expiration date of your paste, the values are explained further down the page
|
|
|
|
|
2018-08-16 20:31:34 -05:00
|
|
|
# example - params = { "api_dev_key": api_dev_key, "api_option": "paste", "api_paste_code": paste_data }
|
2018-08-16 14:54:08 -05:00
|
|
|
def create_paste(params)
|
2018-08-22 13:32:53 -05:00
|
|
|
execute_query(:api_post, params)
|
2018-08-16 14:54:08 -05:00
|
|
|
end
|
|
|
|
|
2018-08-16 20:31:34 -05:00
|
|
|
def get_api_user_key
|
2018-08-16 14:54:08 -05:00
|
|
|
# returns a user session key that can be used as the api_user_key param
|
|
|
|
@response ||= RestClient::Request.execute({
|
|
|
|
method: :post,
|
|
|
|
url: @base_api_url + ENDPOINTS[:login],
|
|
|
|
payload: { 'api_dev_key': @api_dev_key,
|
2018-08-16 20:31:34 -05:00
|
|
|
'api_user_name': @username,
|
|
|
|
'api_user_password': @password }})
|
|
|
|
end
|
|
|
|
|
|
|
|
def list_user_pastes
|
|
|
|
params = { 'api_dev_key': @api_dev_key,
|
|
|
|
'api_user_key': @api_user_key,
|
|
|
|
'api_results_limit': '100',
|
|
|
|
'api_option': 'list'}
|
2018-08-22 13:32:53 -05:00
|
|
|
execute_query(:api_post, params)
|
2018-08-16 20:31:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def api_post(params)
|
|
|
|
response = RestClient::Request.execute(
|
|
|
|
method: :post,
|
|
|
|
url: @base_api_url + ENDPOINTS[:post],
|
|
|
|
payload: params)
|
2018-08-16 14:54:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# params is optional for now. to query specific language ?lang=ruby as an example
|
|
|
|
def scrape_public_pastes(params = nil)
|
|
|
|
response = RestClient::Request.execute(
|
|
|
|
method: :get,
|
|
|
|
url: @scraping_api_url + ENDPOINTS[:scraping])
|
|
|
|
end
|
|
|
|
|
2018-08-22 13:32:53 -05:00
|
|
|
# keep this method private so we are not letting anyone run any method in our program
|
2018-08-19 20:22:48 -05:00
|
|
|
private
|
2018-08-16 14:54:08 -05:00
|
|
|
# this will be the main way to execute any of these methods. this has the exception handling taken care of.
|
|
|
|
def execute_query(selector, *args)
|
|
|
|
begin
|
|
|
|
send(selector, *args)
|
|
|
|
rescue RestClient::ExceptionWithResponse => e
|
|
|
|
puts e.message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
######################## TESTING ####################################################
|
|
|
|
#####################################################################################
|
2018-08-16 20:31:34 -05:00
|
|
|
|
|
|
|
#### INITIAL STEPS
|
2018-08-16 14:54:08 -05:00
|
|
|
|
|
|
|
# setup our object and grab a session key
|
2018-08-16 20:31:34 -05:00
|
|
|
pb = Pastebin::Pastebinner.new(ENV['pastebin_api_key'], ENV['pastebin_username'], ENV['pastebin_password'])
|
2018-08-22 13:32:53 -05:00
|
|
|
api_dev_key = ENV['pastebin_api_key']
|
2018-08-16 14:54:08 -05:00
|
|
|
|
2018-08-16 20:31:34 -05:00
|
|
|
#### CREATE PASTE
|
|
|
|
# prepare some sample paste data to send
|
2018-08-16 14:54:08 -05:00
|
|
|
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 }
|
2018-08-22 13:32:53 -05:00
|
|
|
puts pb.create_paste(params)
|
2018-08-16 20:31:34 -05:00
|
|
|
|
|
|
|
#### SCRAPE PUBLIC PASTES
|
|
|
|
#public_pastes = pb.execute_query(:scrape_public_pastes)
|
|
|
|
#puts public_pastes
|
2018-08-16 14:54:08 -05:00
|
|
|
|
2018-08-16 20:31:34 -05:00
|
|
|
#### LIST USER PASTES
|
|
|
|
#puts pb.execute_query(:list_user_pastes)
|