Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'httparty'
- require 'dotenv/load'
- require 'pp'
- require 'language_sniffer'
- require 'xml/to/hash'
- require 'json'
- module Pastebin
- class Client
- API_POST_URL = "https://pastebin.com/api/api_post.php"
- API_LOGIN_URL = "https://pastebin.com/api/api_login.php"
- API_RAW_URL = "https://pastebin.com/api/api_raw.php"
- def initialize
- @dev_key = ENV['DEV_KEY']
- @username = ENV['USERNAME']
- @password = ENV['PASSWORD']
- @headers = { :header => { "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8" } }
- @user_key = ENV['USER_KEY']
- check_user_key_exists
- end
- def check_user_key_exists
- if @user_key == ""
- @user_key = get_user_key
- env = File.read('./.env')
- env_with_key = env.gsub(/USER_KEY=""/, "USER_KEY=\"#{@user_key}\"")
- File.open('./.env', 'w') { |f| f.puts env_with_key }
- end
- end
- def get_user_key
- body = { :body => { :api_dev_key => @dev_key,
- :api_user_name => @username,
- :api_user_password => @password }
- }
- response = HTTParty.post(API_LOGIN_URL, body)
- response.parsed_response
- end
- def new_paste_from_file(options={})
- body = create_file_paste_body_hash(options)
- payload = @headers.merge(body)
- response = HTTParty.post(API_POST_URL, payload)
- puts response.parsed_response
- end
- def new_paste_from_text(options={}) # TODO options hash?
- body = create_text_paste_body_hash(options)
- payload = @headers.merge(body)
- response = HTTParty.post(API_POST_URL, payload)
- puts response.parsed_response
- end
- def list_pastes(count)
- body = { :body => { :api_dev_key => @dev_key,
- :api_option => "list",
- :api_results_limit => count,
- :api_user_key => @user_key } }
- payload = @headers.merge(body)
- response = HTTParty.post(API_POST_URL, payload)
- xml = Nokogiri::HTML response
- pastes = extract_from_xml(xml)
- end
- def extract_from_xml(xml)
- pastes = Array.new
- xml.xpath('//paste_title').each do |title|
- xml.xpath('//paste_url').each do |url|
- xml.xpath('//paste_key').each do |key|
- pastes.push([title.text,url.text,key.text])
- end
- end
- end
- pastes
- end
- def put_paste_list(pastes)
- pastes.each do |paste|
- puts "#{paste[0]}: #{paste[1]}"
- end
- end
- def get_last_paste(*options)
- last_paste = list_pastes(1)
- last_paste_key = last_paste[0][2]
- body = { :body => { :api_dev_key => @dev_key,
- :api_option => "show_paste",
- :api_paste_key => last_paste_key,
- :api_user_key => @user_key } }
- payload = @headers.merge(body)
- response = HTTParty.post(API_RAW_URL, payload)
- title = last_paste[0][0]
- content = response.parsed_response
- url = last_paste[0][1]
- if options.include?(:raw) # TODO : make this into a hash, not a string
- output = "#{content}"
- else
- output = "#{title}\n\n#{content}\n\n#{url}"
- end
- end
- def list_trending_pastes
- body = { :body => { :api_dev_key => @dev_key,
- :api_option => "trends" } }
- payload = @headers.merge(body)
- response = HTTParty.post(API_POST_URL, payload)
- xml = Nokogiri::HTML response
- pastes = extract_from_xml(xml)
- put_paste_list(pastes)
- end
- def get_paste_format(path)
- begin
- paste_format = LanguageSniffer.detect(path).language.name
- rescue NoMethodError
- "text"
- else
- paste_format
- end
- end
- def create_file_paste_body_hash(options={})
- api_paste_private = options[:api_paste_private] || 0
- path = options[:contents]
- name = options[:name]
- api_paste_format = "text"
- contents = ""
- file = File.open(path, 'r')
- file.each_line do |line|
- contents += line
- end
- body = { :body => { :api_dev_key => @dev_key,
- :api_paste_code => contents,
- :api_paste_private => api_paste_private,
- :api_option => "paste",
- :api_paste_name => File.basename(path),
- :api_paste_format => get_paste_format(path),
- :api_user_key => @user_key } }
- end
- def create_text_paste_body_hash(options={})
- api_paste_private = options[:api_paste_private] || 0
- contents = options[:contents]
- name = options[:name]
- api_paste_format = "text"
- body = { :body => { :api_dev_key => @dev_key,
- :api_paste_code => contents,
- :api_paste_private => api_paste_private,
- :api_option => "paste",
- :api_paste_name => name,
- :api_paste_format => api_paste_format,
- :api_user_key => @user_key } }
- end
- end
- end
- pastebin = Pastebin::Client.new
- pastebin.new_paste_from_file({:api_paste_private => '0', :contents => './pastebin.rb'})
- #~# TODO #~#
- # make a package: https://github.com/gosu/releasy
- # add help docs
- # fix messy body hashes
- # make it possible to send any number (or zero) parameters
- ## to the function.
- # tests:
- # pasteit.list_trending_pastes
- # pasteit.new_paste_from_file('/home/benjamin/Dev/pasteit/testfile.txt')
- # pasteit.new_paste_from_text("HELLO_WORLD_2","tezt")
Add Comment
Please, Sign In to add comment