Class: MozAPI

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/mozapi.rb

Constant Summary collapse

GLOBAL_LIMIT =

there is a global limit on the Mozscape API that only lets you retrieve 100,000 links for any URL

100000
LIMIT =
100
DEFAULT_SOURCE_COLS =

URL + root_domain + page_authority + domain_authority

4 + 16 + 34359738368 + 68719476736
DEFAULT_TARGET_COLS =

URL + root_domain

4 + 16
4

Instance Method Summary collapse

Constructor Details

#initializeMozAPI

Returns a new instance of MozAPI.



31
32
33
34
# File 'lib/mozapi.rb', line 31

def initialize
  @api_id = ENV['MOZ_API_ID']
  @api_key = ENV['MOZ_API_KEY']
end

Instance Method Details

#calculate_signature(expires) ⇒ Object

private



65
66
67
68
69
70
71
# File 'lib/mozapi.rb', line 65

def calculate_signature(expires)
  signature = "#{@api_id}\n#{expires}"
  digest = OpenSSL::HMAC.digest('sha1', @api_key, signature)
      
  b64 = Digest::HMAC.base64digest(signature, @api_key, Digest::SHA1)
  CGI::escape(Base64.encode64(digest).chomp)
end


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/mozapi.rb', line 36

def links(target_url, options)
  sleep(10) # do this to honor the API rate limit
  # add 5 minutes
  expires = (Time.now + 5 * 60).utc.to_i
  
  options = {
    Sort: 'page_authority',
    Filter: 'follow+external',
    SourceCols: DEFAULT_SOURCE_COLS,
    TargetCols: DEFAULT_TARGET_COLS,
    LinkCols: DEFAULT_LINK_COLS,
    AccessID: @api_id,
    Expires: expires,
    Signature: calculate_signature(expires),
    Limit: LIMIT,
    Offset: 0
  }.merge(options)
  
  #puts "[MozAPI#links] options: #{options[:Offset]}"
  req_url = "http://lsapi.seomoz.com/linkscape/links/#{URI::encode(target_url)}?#{options.to_query}" 

  response = HTTParty.get(req_url, :headers => {"User-Agent" => 'node-linkscape (https://github.com/mjp/node-linkscape)'})
  json = JSON.parse response.body
  puts "[MozAPI#links] links returned: #{json.size}"
  
  json
end