205 lines
8.4 KiB
Ruby
205 lines
8.4 KiB
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
|
|
|
|
module Pastebinner
|
|
class ApiClient
|
|
# A hash of every endpoint that the Pastebinner::Api Client will use
|
|
ENDPOINTS =
|
|
{
|
|
login: '/api_login.php',
|
|
post: '/api_post.php',
|
|
raw: '/api_raw.php',
|
|
scraping: '/api_scraping.php',
|
|
scrape_item: '/api_scrape_item.php',
|
|
scrape_item_meta: '/api_scrape_item_meta.php'
|
|
}.freeze
|
|
|
|
# The pastebin pro api developer key
|
|
# @return [String] '123456789'
|
|
attr_accessor :api_dev_key
|
|
# The pastebin user account name
|
|
# @return [String] 'johnsmith'
|
|
attr_accessor :username
|
|
# The pastebin user accounts password
|
|
# @return [String] 'password123'
|
|
attr_accessor :password
|
|
|
|
# Creates a new instance of Pastebinner::ApiClient
|
|
# @param api_dev_key [Symbol] The pastebin pro api developer key
|
|
# @param username [String] The pastebin user account name
|
|
# @param password [String] The pastebin user accounts password
|
|
# @note The Official Pastebin API docs can be located at the following url: https://pastebin.com/api
|
|
# @return [Pastebinner::ApiClient] A new Pastebinner::ApiClient object
|
|
# @example Create an object
|
|
# api_client = Pastebinner::ApiClient.new('123456789', 'johnsmith', 'password123')
|
|
def initialize(api_dev_key, username, password)
|
|
@api_dev_key = api_dev_key
|
|
@username = username
|
|
@password = password
|
|
@base_api_url = 'https://pastebin.com/api'
|
|
@scraping_api_url = 'https://scrape.pastebin.com'
|
|
end
|
|
|
|
# @param api_dev_key [String] Required: Your unique developer api key
|
|
# @param api_option [String] Required: Set as paste, this will indicate you want to create a new paste
|
|
# @param api_paste_code [String] Required: This is the text that will be written inside of your paste
|
|
# @param api_user_key [String] Optional: This parameter is part of the login system, which is explained further down the page
|
|
# @param api_paste_name [String] Optional: This will be the name / title of your paste
|
|
# @param api_paste_format [String] Optional: This will be the syntax highlighting value, which is explained in detail further down the page
|
|
# @param api_paste_private [String] Optional: This makes a paste public, private, or unlisted - public = 0, unlisted = 1, private = 2
|
|
# @param api_paste_expire_date [String] Optional: This sets the expiration date of your paste, the values are explained further down the page
|
|
# @example - params = { "api_dev_key": '1234567', "api_option": "paste", "api_paste_code": 'An example of some paste data' }
|
|
def create_paste(params)
|
|
execute_query(:api_post, params)
|
|
end
|
|
|
|
# Sends a POST request to the /api_login.php endpoint to establish a user session key for future posts.
|
|
# @return [String] A user session key that can be used as the api_user_key param
|
|
def api_user_key
|
|
@api_user_key ||= RestClient::Request.execute(
|
|
method: :post,
|
|
url: @base_api_url + ENDPOINTS[:login],
|
|
payload: { 'api_dev_key': @api_dev_key,
|
|
'api_user_name': @username,
|
|
'api_user_password': @password }
|
|
)
|
|
end
|
|
# Sends a POST request to the /api_post.php endpoint that will list the first 100 pastes for the current logged in user
|
|
# @return [RestClient::Response] object
|
|
def list_user_pastes
|
|
params = { 'api_dev_key': api_dev_key,
|
|
'api_user_key': api_user_key,
|
|
'api_results_limit': '100',
|
|
'api_option': 'list' }
|
|
execute_query(:api_post, params)
|
|
end
|
|
|
|
# List the raw user paste data. Sends a POST request to the /api_post.php endpoint with the 'show_paste' option passed as a parameter
|
|
# @param api_paste_key [String] This is the unique key of the paste data that you want to list
|
|
# @return [RestClient::Response] object
|
|
def list_raw_user_paste(api_paste_key)
|
|
params = { 'api_dev_key': api_dev_key,
|
|
'api_user_key': api_user_key,
|
|
'api_paste_key': api_paste_key,
|
|
'api_option': 'show_paste' }
|
|
execute_query(:api_post, params)
|
|
end
|
|
|
|
# Delete a paste. Sends a POST request to the /api_post.php endpoint with the 'delete' option passed. Expects a key of a paste.
|
|
# @param api_paste_key - This is the unique key of the paste data you want to delete
|
|
# @return [RestClient::Response] object
|
|
def delete_user_paste(api_paste_key)
|
|
params = { 'api_dev_key': api_dev_key,
|
|
'api_user_key': api_user_key,
|
|
'api_paste_key': api_paste_key,
|
|
'api_option': 'delete' }
|
|
execute_query(:api_post, params)
|
|
end
|
|
|
|
|
|
|
|
# A hash of the api token
|
|
def get_user_info
|
|
params = { 'api_dev_key': api_dev_key }
|
|
end
|
|
|
|
# Send a POST request to the /api_post.php endpoint.
|
|
# @param params [Hash] object
|
|
# @return [RestClient::Response] object
|
|
def api_post(params)
|
|
response = RestClient::Request.execute(
|
|
method: :post,
|
|
url: @base_api_url + ENDPOINTS[:post],
|
|
payload: params
|
|
)
|
|
end
|
|
|
|
# Scrape public pastes can specify specific language with options
|
|
# @param params DOES NOT WORK - optional, specific language query with ?lang=ruby as an example.
|
|
# @param limit the number of pastes to scrape
|
|
# @return [RestClient::Response] object
|
|
def scrape_public_pastes(_params = nil, limit)
|
|
response = RestClient::Request.execute(
|
|
method: :get,
|
|
url: @scraping_api_url + ENDPOINTS[:scraping] + "?limit=#{limit}"
|
|
)
|
|
end
|
|
|
|
# Extracts just the keys from recent public pastes.
|
|
# @param public_pastes [Restclient::Resposne, Restclient::Response] An array of public paste responses returned from the scrape_public_pastes method.
|
|
# @return ['paste_key1, paste_key2, paste_key3'] An Array of Strings that are unique paste keys.
|
|
def get_unique_paste_keys(public_pastes)
|
|
pp = JSON.parse(public_pastes)
|
|
pp.map { |p| p['key'] }
|
|
end
|
|
|
|
# Sends a GET request to the scraping API with the unique paste key of the paste intended to download
|
|
# @param unique_paste_key [String] The unique paste key of the paste
|
|
# @return [RestClient::Response] object
|
|
def raw_paste_data(unique_paste_key)
|
|
response = RestClient::Request.execute(
|
|
method: :get,
|
|
url: @scraping_api_url + ENDPOINTS[:scrape_item] + "?i=#{unique_paste_key}"
|
|
)
|
|
end
|
|
|
|
# Sends a GET request to the scraping API with the unique paste key of the paste intended. Pulls metadata info.
|
|
# @param raw_paste_metadata [String] The unique paste key of the paste
|
|
# @return [Json object?] need to test this.
|
|
def raw_paste_metadata(unique_paste_key)
|
|
response = RestClient::Request.execute(
|
|
method: :get,
|
|
url: @scraping_api_url + ENDPOINTS[:scrape_item_meta] + "?i=#{unique_paste_key}"
|
|
)
|
|
YAML.safe_load(response.body).first
|
|
end
|
|
|
|
def hash_pastes(keys)
|
|
if keys.is_a? String
|
|
raw_paste = self.raw_paste_data(keys)
|
|
raw_paste_metadata = self.raw_paste_metadata(keys)
|
|
hash = self.hash_paste(raw_paste, raw_paste_metadata)
|
|
else
|
|
keys.map do |key|
|
|
raw_paste = self.raw_paste_data(key).body
|
|
raw_paste_metadata = self.raw_paste_metadata(key)
|
|
hash = self.hash_paste(raw_paste, raw_paste_metadata)
|
|
end
|
|
end
|
|
end
|
|
|
|
def hash_paste(raw_paste_data, raw_paste_metadata)
|
|
{ "paste_metadata": raw_paste_metadata,
|
|
"paste_text": raw_paste_data }
|
|
end
|
|
|
|
def json_paste(key=nil, keys)
|
|
# if we give keys, create an array of X json pastes
|
|
if keys
|
|
self.hash_pastes(keys).map do |paste_hash|
|
|
paste_hash.to_json
|
|
end
|
|
else
|
|
# otherwise, just make a json of the 1 raw_paste_data & raw_paste_metadata
|
|
raw_paste_data = self.raw_paste_data(key)
|
|
raw_paste_metadata = self.raw_paste_metadata(key)
|
|
self.hash_paste(raw_paste_data, raw_paste_metadata).to_json
|
|
end
|
|
end
|
|
|
|
def json_paste_from_file(raw_paste_json_file)
|
|
raw_paste_json = File.read(raw_paste_json_file)
|
|
self.hash_paste(raw_paste_json).to_json
|
|
end
|
|
|
|
private
|
|
|
|
# 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)
|
|
send(selector, *args)
|
|
rescue RestClient::ExceptionWithResponse => e
|
|
puts e.message
|
|
end
|
|
end
|
|
end
|