<commit_before>module Nzbmatrix
  class Client

    require "rest_client"
    
    ENDPOINT = "http://nzb-matrix.eu"

    attr_accessor :user_id, :api_key, :endpoint
    
    def initialize(user_id, api_key, endpoint = ENDPOINT)
      @user_id = user_id
      @api_key = api_key
      @endpoint = endpoint
    end


    def get(*args)
      RestClient.get(*args)
    end
    
    def api_request(function, params)
      params = params.merge(:t => function, :apikey => @api_key)
      get "#{endpoint}/api", params: params
    end

    def search(query, options = {})
      # FIXME: provide a nicer interface so that users don't have to
      # know category ids (and so forth)
      params = options.merge(:q => query)
      response = api_request(:search, params)
      Parser.new.call(response)
    end

    def get_nzb(guid, options = {})
      params = options.merge(:i => user_id, :r => api_key)
      get "#{endpoint}/getnzb/#{guid}.nzb", params: params
    end
  end
end<commit_msg>Extract the nzb filename after downloading it<commit_after>module Nzbmatrix
  class Client

    require "rest_client"
    
    ENDPOINT = "http://nzb-matrix.eu"

    attr_accessor :user_id, :api_key, :endpoint
    
    def initialize(user_id, api_key, endpoint = ENDPOINT)
      @user_id = user_id
      @api_key = api_key
      @endpoint = endpoint
    end


    def get(*args)
      RestClient.get(*args)
    end
    
    def api_request(function, params)
      params = params.merge(:t => function, :apikey => @api_key)
      get "#{endpoint}/api", params: params
    end

    def search(query, options = {})
      # FIXME: provide a nicer interface so that users don't have to
      # know category ids (and so forth)
      params = options.merge(:q => query)
      response = api_request(:search, params)
      Parser.new.call(response)
    end

    def get_nzb(guid, options = {})
      params = options.merge(:i => user_id, :r => api_key)
      content = get("#{endpoint}/getnzb/#{guid}.nzb", params: params)

      # FIXME: Can we get the name from the HTTP headers and save on
      # parsing the document? If so, is this a good idea? Maybe the
      # name in the document is more correct, or maying parsing it
      # will show up a parsing error earlier that you might otherwise
      # detect it.
      
      document = Nokogiri::XML(content)
      filename = document.at_css("meta[type=name]").text
      [filename, content]
    end
  end
end
{'fraction_non_alphanumeric': 0.10870453616673478, 'fraction_numerical': 0.0, 'mean_word_length': 2.637444279346211, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1515419', 'n_tokens_mistral': 769, 'n_tokens_neox': 753, 'n_words': 228}