Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
fitomad committed Feb 8, 2023
1 parent 989a33d commit 1e68911
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 252 deletions.
1 change: 0 additions & 1 deletion src/genres/__init__.py

This file was deleted.

50 changes: 0 additions & 50 deletions src/genres/mac.py

This file was deleted.

62 changes: 11 additions & 51 deletions src/iosr3.py → src/ios-ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@
# -*- coding: utf-8 -*-

import sys
import json
import http.client
import datetime
import argparse

from genres import ios
from util import countries, platforms

store_server = str("itunes.apple.com")
from util import countries, platforms, genres, common


def ranking_free(iso_code, platform, genre):
Expand All @@ -26,9 +20,9 @@ def ranking_free(iso_code, platform, genre):
else:
store_section = "FreeAppleTVApps"

store_uri = common.make_store_uri(iso_code, genre, store_section)

store_uri = "/WebObjects/MZStoreServices.woa/ws/charts?cc={0}&g={1}&name={2}&limit=400".format(iso_code, genre, store_section)
return request_url(store_uri)
return common.fetch(store_uri, AppID)


def ranking_paid(iso_code, platform, genre):
Expand All @@ -42,9 +36,9 @@ def ranking_paid(iso_code, platform, genre):
else:
store_section = "PaidAppleTVApps"

store_uri = "/WebObjects/MZStoreServices.woa/ws/charts?cc={0}&g={1}&name={2}&limit=400".format(iso_code, genre, store_section)
return request_url(store_uri)
store_uri = common.make_store_uri(iso_code, genre, store_section)

return common.fetch(store_uri, AppID)


def ranking_grossing(iso_code, platform, genre):
Expand All @@ -58,43 +52,9 @@ def ranking_grossing(iso_code, platform, genre):
else:
store_section = "AppleTVAppsByRevenue"

store_uri = f"/WebObjects/MZStoreServices.woa/ws/charts?cc={iso_code}&g={genre}&name={store_section}&limit=400"
store_uri = common.make_store_uri(iso_code, genre, store_section)

return request_url(store_uri)


def request_url(url):
"""
Request an URL
"""
conn = http.client.HTTPSConnection(store_server)
headers = { "Cache-Control" : "no-cache" }
conn.request("GET", url, None, headers)
r1 = conn.getresponse()

data1 = r1.read()

ranking = json.loads(data1)

conn.close()

# JSON document
posicion = process_result(ranking)

return posicion


def process_result(result):
"""
"""
entries = result["resultIds"]

try:
position = entries.index(AppID)
return (position + 1)
except ValueError:
return "---"
return common.fetch(store_uri, AppID)


def print_parameters():
Expand All @@ -103,7 +63,7 @@ def print_parameters():
"""
print("\r\n\t{0:^26}\r\n".format("App Store Categories"))

for genre in ios.AppStoreGenre:
for genre in genres.AppStoreGenre:
print("\t{0:^20} = {1:<6}".format(genre.name, genre.value))


Expand All @@ -116,7 +76,7 @@ def print_parameters():
print("\t{0:^20} = {1:<6}".format("iPad", "ipad"))
print("\t{0:^20} = {1:<6}\r\n".format("Apple TV", "appleTV"))

print("\tExample: iosr.py -appid 123456789 -category 12014 -stores top -platform iphone\r\n")
print("\tExample: ios-ranking.py -appid 123456789 -category 12014 -stores top -platform iphone\r\n")

#
# Workflow
Expand Down Expand Up @@ -159,7 +119,7 @@ def print_parameters():
# App category

try:
genre = ios.AppStoreGenre(args.category)
genre = genres.AppStoreGenre(args.category)
except ValueError:
print_parameters()
print("\tCategory not available. Take a look at category section above.\r\n")
Expand Down
87 changes: 13 additions & 74 deletions src/macr3.py → src/mac-ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,19 @@
# -*- coding: utf-8 -*-

import sys
import json
import http.client
import datetime
import argparse

from genres import mac
from util import countries
from util import countries, genres, common

store_server = str("itunes.apple.com")

def make_store_uri(country_code: str, genre: str, section: str) -> str:
store_uri = f"/WebObjects/MZStoreServices.woa/ws/charts?cc={country_code}&g={genre}&name={section}&limit=400"

return store_uri



def ranking_free(iso_code):
"""
Top free Mac app ranking
"""
store_uri = make_store_uri(iso_code, str(genre.value), "FreeMacApps")

return request_url(store_uri)


def ranking_paid(iso_code):
"""
Top paid Mac applications
"""
store_uri = make_store_uri(iso_code, str(genre.value), "PaidMacApps")

return request_url(store_uri)


def ranking_grossing(iso_code):
def ranking(iso_code, section):
"""
Top grossing Mac applications
Mac App Store ranking for a country and a section
"""
store_uri = make_store_uri(iso_code, str(genre.value), "MacAppsByRevenue")

return request_url(store_uri)

"""
Request an URL
"""
def request_url(url):
conn = http.client.HTTPSConnection(store_server)
headers = { "Cache-Control" : "no-cache" }
conn.request("GET", url, None, headers)
r1 = conn.getresponse()

data1 = r1.read()

ranking = json.loads(data1)

conn.close()

# JSON document
posicion = process_result(ranking)

return posicion

"""
Process JSON and recover results
"""
def process_result(result):
entries = result["resultIds"]
store_uri = common.make_store_uri(iso_code, str(genre.value), section)
position = common.fetch(store_uri, AppID)

try:
position = entries.index(AppID)
return (position + 1)
except ValueError:
return "---"
return position


def print_parameters():
Expand All @@ -84,15 +23,15 @@ def print_parameters():
"""
print("\r\n\t{0:^26}\r\n".format("Mac App Store Categories"))

for genre in mac.MacAppStoreGenre:
for genre in genres.MacAppStoreGenre:
print("\t{0:^20} = {1:<6}".format(genre.name, genre.value))


print("\r\n\t{0:^26}\r\n".format("Countries availables"))
print("\t{0:^20} = {1:<6}".format("All countries", "world"))
print("\t{0:^20} = {1:<6}\r\n".format("Top 10 (by revenue)", "top"))

print("\tExample: macr.py -appid 123456789 -category 12014 -stores top\r\n")
print("\tExample: mac-ranking.py -appid 123456789 -category 12014 -stores top\r\n")

#
# Workflow.
Expand Down Expand Up @@ -133,7 +72,7 @@ def print_parameters():

# App category
try:
genre = mac.MacAppStoreGenre(args.category)
genre = genres.MacAppStoreGenre(args.category)
except ValueError:
print_parameters()
print("\tCategory not available. Take a look at category section above.\r\n")
Expand All @@ -148,11 +87,11 @@ def print_parameters():
print("\t{0:20}{1:>10}{2:>10}{3:>10}\r\n".format("Country", "Free", "Paid", "Grossing"))

for country in store_countries:
posicion_free = ranking_free(country[1])
posicion_paid = ranking_paid(country[1])
posicion_grossing = ranking_grossing(country[1])
position_free = ranking(country[1], "FreeMacApps")
position_paid = ranking(country[1], "PaidMacApps")
position_grossing = ranking(country[1], "MacAppsByRevenue")

print("\t{0:20.17}{1:>10}{2:>10}{3:>10}".format(country[2], posicion_free, posicion_paid, posicion_grossing))
print("\t{0:20.17}{1:>10}{2:>10}{3:>10}".format(country[2], position_free, position_paid, position_grossing))

print("\r\n\t{0:^50}".format("A script by @fitomad."))
print("\t{0:^50}\r\n".format("GitHub: https://github.com/fitomad/App-Store-Ranking"))
46 changes: 46 additions & 0 deletions src/util/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import http.client
import json

store_server = str("itunes.apple.com")

def make_store_uri(country_code: str, genre: str, section: str) -> str:
"""
"""
store_uri = f"/WebObjects/MZStoreServices.woa/ws/charts?cc={country_code}&g={genre}&name={section}&limit=400"

return store_uri


def fetch(url, appID):
"""
Request an URL
"""
conn = http.client.HTTPSConnection(store_server)
headers = { "Cache-Control" : "no-cache" }
conn.request("GET", url, None, headers)
response = conn.getresponse()

data = response.read()

ranking = json.loads(data)

conn.close()

# JSON document
posicion = process_result(ranking, appID)

return posicion


def process_result(result, appID):
"""
Process JSON and recover results
"""
entries = result["resultIds"]

try:
position = entries.index(appID)
return (position + 1)
except ValueError:
return "---"
Loading

0 comments on commit 1e68911

Please sign in to comment.