Skip to content

Instantly share code, notes, and snippets.

@k0nserv
Last active August 29, 2015 14:18
Show Gist options
  • Save k0nserv/adc136f35c696584c0aa to your computer and use it in GitHub Desktop.
Save k0nserv/adc136f35c696584c0aa to your computer and use it in GitHub Desktop.
Find your apps ranking in the app store for the apps keywords and other searchterms
require 'net/http'
require 'uri'
require 'json'
BASE_URL = 'https://itunes.apple.com/search'
BUNDLE_ID = nil
STORE_COUNTRY_CODE = 'us'
LIMIT = 200
# Set to true to try combining every keyword
# with every other
COMBINATORIAL = false
def halt(message, code = 1)
print message
exit code
end
def main
halt('BUNDLE_ID is not defined') unless BUNDLE_ID
halt('Missing atleast one keywrd to search for') if ARGV.empty?
terms = ARGV.map { |string| string.split(',') }.flatten
if COMBINATORIAL && terms.count > 1
terms = terms.map do |term|
terms.map do |other_term|
next if other_term == term
"#{term} #{other_term}"
end
end.flatten.reject(&:nil?)
end
print "Analyzing #{terms.count} search terms in #{STORE_COUNTRY_CODE} store\n"
progress = 1
ranking = terms.map do |term|
result = rank_with_term(term)
print "#{progress} of #{terms.count} done\n"
progress += 1
result
end
sorted_ranking = ranking.sort do |a, b|
a[0] && b[0] ? a[0] <=> b[0] : a[0] ? -1 : 1
end
print_ranking(sorted_ranking)
end
def print_ranking(ranking)
print "Ranking of app with bundle id #{BUNDLE_ID} in #{STORE_COUNTRY_CODE} store\n"
ranking.each do |rank|
position = rank[0]
term = rank[1]
if position.nil?
print "For Term: #{term} the app was not in the first #{LIMIT} search results\n"
else
print "For Term: #{term} the app was ranked #{position}\n"
end
end
end
def rank_with_term(term)
uri = URI.parse(BASE_URL)
uri.query = URI.encode_www_form(term: term,
media: 'software',
country: STORE_COUNTRY_CODE,
limit: LIMIT )
response = Net::HTTP.get_response(uri)
data = JSON.parse(response.body, symbolize_names: true)
position = nil
data[:results].each_with_index do |result, index|
if result[:bundleId] == BUNDLE_ID
position = index + 1
break
end
end
[position, term]
end
main
$ ruby app_store.rb keyword,keyword2,keyword3,app-name,parts-of-title
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment