benjb

pastebin.rb

Aug 5th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.45 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'httparty'
  4. require 'dotenv/load'
  5. require 'pp'
  6. require 'language_sniffer'
  7. require 'xml/to/hash'
  8. require 'json'
  9.  
  10. module Pastebin
  11.   class Client
  12.  
  13.       API_POST_URL = "https://pastebin.com/api/api_post.php"
  14.       API_LOGIN_URL = "https://pastebin.com/api/api_login.php"
  15.       API_RAW_URL = "https://pastebin.com/api/api_raw.php"
  16.  
  17.     def initialize
  18.       @dev_key = ENV['DEV_KEY']
  19.       @username = ENV['USERNAME']
  20.       @password = ENV['PASSWORD']
  21.       @headers = { :header => { "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8" } }
  22.       @user_key = ENV['USER_KEY']
  23.       check_user_key_exists
  24.     end
  25.  
  26.     def check_user_key_exists
  27.  
  28.       if @user_key == ""
  29.           @user_key = get_user_key
  30.           env = File.read('./.env')
  31.           env_with_key = env.gsub(/USER_KEY=""/, "USER_KEY=\"#{@user_key}\"")
  32.           File.open('./.env', 'w') { |f| f.puts env_with_key }
  33.       end
  34.  
  35.     end
  36.  
  37.     def get_user_key
  38.  
  39.       body = { :body => { :api_dev_key => @dev_key,
  40.                         :api_user_name => @username,
  41.                         :api_user_password => @password }
  42.              }
  43.  
  44.       response = HTTParty.post(API_LOGIN_URL, body)
  45.       response.parsed_response
  46.     end
  47.  
  48.     def new_paste_from_file(options={})
  49.  
  50.      body = create_file_paste_body_hash(options)
  51.  
  52.      payload = @headers.merge(body)
  53.      response = HTTParty.post(API_POST_URL, payload)
  54.      puts response.parsed_response
  55.  
  56.     end
  57.  
  58.     def new_paste_from_text(options={}) # TODO options hash?
  59.  
  60.      body = create_text_paste_body_hash(options)
  61.  
  62.      payload = @headers.merge(body)
  63.      response = HTTParty.post(API_POST_URL, payload)
  64.      puts response.parsed_response
  65.     end
  66.  
  67.     def list_pastes(count)
  68.         body = { :body => {  :api_dev_key => @dev_key,
  69.                   :api_option => "list",
  70.                   :api_results_limit => count,
  71.                   :api_user_key =>  @user_key } }
  72.  
  73.         payload = @headers.merge(body)
  74.         response = HTTParty.post(API_POST_URL, payload)
  75.         xml = Nokogiri::HTML response
  76.  
  77.         pastes = extract_from_xml(xml)
  78.     end
  79.  
  80.     def extract_from_xml(xml)
  81.       pastes = Array.new
  82.  
  83.         xml.xpath('//paste_title').each do |title|
  84.           xml.xpath('//paste_url').each do |url|
  85.             xml.xpath('//paste_key').each do |key|
  86.             pastes.push([title.text,url.text,key.text])
  87.           end
  88.         end
  89.       end
  90.       pastes
  91.     end
  92.  
  93.     def put_paste_list(pastes)
  94.       pastes.each do |paste|
  95.           puts "#{paste[0]}: #{paste[1]}"
  96.         end
  97.     end
  98.  
  99.     def get_last_paste(*options)
  100.  
  101.       last_paste = list_pastes(1)
  102.       last_paste_key = last_paste[0][2]
  103.  
  104.  
  105.       body = { :body => {  :api_dev_key => @dev_key,
  106.                   :api_option => "show_paste",
  107.                   :api_paste_key => last_paste_key,
  108.                   :api_user_key =>  @user_key } }
  109.  
  110.  
  111.       payload = @headers.merge(body)
  112.       response = HTTParty.post(API_RAW_URL, payload)
  113.  
  114.       title = last_paste[0][0]
  115.       content = response.parsed_response
  116.       url = last_paste[0][1]
  117.  
  118.       if options.include?(:raw) # TODO : make this into a hash, not a string
  119.         output = "#{content}"
  120.       else
  121.         output = "#{title}\n\n#{content}\n\n#{url}"
  122.       end
  123.     end
  124.  
  125.     def list_trending_pastes
  126.  
  127.         body = { :body => {  :api_dev_key => @dev_key,
  128.                   :api_option => "trends" } }
  129.  
  130.         payload = @headers.merge(body)
  131.         response = HTTParty.post(API_POST_URL, payload)
  132.  
  133.         xml = Nokogiri::HTML response
  134.  
  135.         pastes = extract_from_xml(xml)
  136.         put_paste_list(pastes)
  137.     end
  138.  
  139.     def get_paste_format(path)
  140.  
  141.         begin
  142.             paste_format = LanguageSniffer.detect(path).language.name
  143.         rescue NoMethodError
  144.            "text"
  145.         else
  146.            paste_format
  147.        end
  148.     end
  149.  
  150.     def create_file_paste_body_hash(options={})
  151.  
  152.       api_paste_private = options[:api_paste_private] || 0
  153.       path = options[:contents]
  154.       name = options[:name]
  155.       api_paste_format = "text"
  156.       contents = ""
  157.       file = File.open(path, 'r')
  158.  
  159.       file.each_line do |line|
  160.         contents += line
  161.       end
  162.  
  163.       body = { :body => {  :api_dev_key => @dev_key,
  164.                   :api_paste_code => contents,
  165.                   :api_paste_private => api_paste_private,
  166.                   :api_option => "paste",
  167.                   :api_paste_name => File.basename(path),
  168.                   :api_paste_format => get_paste_format(path),
  169.                   :api_user_key =>  @user_key } }
  170.     end
  171.  
  172.  
  173.     def create_text_paste_body_hash(options={})
  174.  
  175.       api_paste_private = options[:api_paste_private] || 0
  176.       contents = options[:contents]
  177.       name = options[:name]
  178.       api_paste_format = "text"
  179.  
  180.       body = { :body => {  :api_dev_key => @dev_key,
  181.                   :api_paste_code => contents,
  182.                   :api_paste_private => api_paste_private,
  183.                   :api_option => "paste",
  184.                   :api_paste_name => name,
  185.                   :api_paste_format => api_paste_format,
  186.                   :api_user_key =>  @user_key } }
  187.   end
  188. end
  189. end
  190.  
  191. pastebin = Pastebin::Client.new
  192. pastebin.new_paste_from_file({:api_paste_private => '0', :contents => './pastebin.rb'})
  193.  
  194.                         #~# TODO #~#
  195.  
  196. # make a package: https://github.com/gosu/releasy
  197. # add help docs
  198. # fix messy body hashes
  199. # make it possible to send any number (or zero) parameters
  200. ## to the function.
  201.  
  202. # tests:
  203.  
  204. # pasteit.list_trending_pastes
  205. # pasteit.new_paste_from_file('/home/benjamin/Dev/pasteit/testfile.txt')
  206. # pasteit.new_paste_from_text("HELLO_WORLD_2","tezt")
Add Comment
Please, Sign In to add comment