diff --git a/src/genres/__init__.py b/src/genres/__init__.py deleted file mode 100644 index 71c30d2..0000000 --- a/src/genres/__init__.py +++ /dev/null @@ -1 +0,0 @@ -#Empty. diff --git a/src/genres/mac.py b/src/genres/mac.py deleted file mode 100644 index e6258bb..0000000 --- a/src/genres/mac.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -from enum import Enum, unique - -# -# Codigos de categoría para la Mac App Store -# -@unique -class MacAppStoreGenre(Enum): - Business = 2001 - Developer = 12002 - Education = 12003 - Entertainment = 12004 - Finance = 12005 - Games = 12006 - Game_Action = 12201 - Game_Adventure = 12202 - Game_Arcade = 12203 - Game_Board = 12204 - Game_Card = 12205 - Game_Casino = 12206 - Game_Dice = 12207 - Game_Educational = 12208 - Game_Family = 12209 - Game_Kids = 12210 - Game_Music = 12211 - Game_Puzzle = 12212 - Game_Racing = 12213 - Game_RolePlaying = 12214 - Game_Simulation = 12215 - Game_Sports = 12216 - Game_Strategy = 12217 - Game_Trivia = 12218 - Game_Word = 12219 - Health = 2007 - Lifestyle = 12008 - Medical = 12010 - Music = 12011 - News = 12012 - Photography = 12013 - Productivity = 12014 - Reference = 12015 - Social = 12016 - Sports = 12017 - Travel = 12018 - Utilities = 12019 - Video = 12020 - Weather = 12021 - Design = 12022 diff --git a/src/iosr3.py b/src/ios-ranking.py similarity index 73% rename from src/iosr3.py rename to src/ios-ranking.py index 52143f9..fded070 100644 --- a/src/iosr3.py +++ b/src/ios-ranking.py @@ -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): @@ -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): @@ -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): @@ -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(): @@ -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)) @@ -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 @@ -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") diff --git a/src/macr3.py b/src/mac-ranking.py similarity index 51% rename from src/macr3.py rename to src/mac-ranking.py index 4c915bc..69d4499 100644 --- a/src/macr3.py +++ b/src/mac-ranking.py @@ -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(): @@ -84,7 +23,7 @@ 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)) @@ -92,7 +31,7 @@ def print_parameters(): 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. @@ -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") @@ -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")) \ No newline at end of file diff --git a/src/util/common.py b/src/util/common.py new file mode 100644 index 0000000..8f96c63 --- /dev/null +++ b/src/util/common.py @@ -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 "---" \ No newline at end of file diff --git a/src/genres/ios.py b/src/util/genres.py similarity index 64% rename from src/genres/ios.py rename to src/util/genres.py index 0a02cc4..167e29b 100644 --- a/src/genres/ios.py +++ b/src/util/genres.py @@ -1,76 +1,119 @@ -from enum import Enum, unique - -# -# Genres codes for App Store -# -@unique -class AppStoreGenre(Enum): - Business = 6000 - Weather = 6001 - Utilities = 6002 - Travel = 6003 - Sports = 6004 - SocialNetworking = 6005 - Reference = 6006 - Productivity = 6007 - PhotoVideo = 6008 - News = 6009 - Navigation = 6010 - Music = 6011 - Lifestyle = 6012 - Games_HealthFitness = 6013 - Games_Games = 6014 - Games_Games_Action = 7001 - Games_Games_Adventure = 7002 - Games_Arcade = 7003 - Games_Board = 7004 - Games_Card = 7005 - Games_Casino = 7006 - Games_Dice = 7007 - Games_Educational = 7008 - Games_Family = 7009 - Games_Kids = 7010 - Games_Music = 7011 - Games_Puzzle = 7012 - Games_Racing = 7013 - Games_RolePlaying = 7014 - Games_Simulation = 7015 - Games_Sports = 7016 - Games_Strategy = 7017 - Games_Trivia = 7018 - Games_Word = 7019 - Finance = 6015 - Entertainment = 6016 - Education = 6017 - Books = 6018 - Medical = 6020 - Newsstand = 6021 - NewsPolitics = 13001 - FashionStyle = 13002 - HomeGarden = 13003 - OutdoorsNature = 13004 - SportsLeisure = 13005 - Automotive = 13006 - ArtsPhotography = 13007 - BridesWeddings = 13008 - BusinessInvesting = 13009 - ChildrensMagazines = 13010 - ComputersInternet = 13011 - CookingFoodDrink = 13012 - CraftsHobbies = 13013 - ElectronicsAudio = 13014 - Entertainment = 13015 - HealthMindBody = 13017 - History = 13018 - LiteraryMagazinesJournals = 13019 - MensInterest = 13020 - MoviesMusic = 13021 - ParentingFamily = 13023 - Pets = 13024 - ProfessionalTrade = 13025 - RegionalNews = 13026 - Science = 13027 - Teens = 13028 - TravelRegional = 13029 - WomensInterest = 13030 - Catalogs = 6022 +from enum import Enum, unique + +# +# Genres codes for App Store +# +@unique +class AppStoreGenre(Enum): + Business = 6000 + Weather = 6001 + Utilities = 6002 + Travel = 6003 + Sports = 6004 + SocialNetworking = 6005 + Reference = 6006 + Productivity = 6007 + PhotoVideo = 6008 + News = 6009 + Navigation = 6010 + Music = 6011 + Lifestyle = 6012 + Games_HealthFitness = 6013 + Games_Games = 6014 + Games_Games_Action = 7001 + Games_Games_Adventure = 7002 + Games_Arcade = 7003 + Games_Board = 7004 + Games_Card = 7005 + Games_Casino = 7006 + Games_Dice = 7007 + Games_Educational = 7008 + Games_Family = 7009 + Games_Kids = 7010 + Games_Music = 7011 + Games_Puzzle = 7012 + Games_Racing = 7013 + Games_RolePlaying = 7014 + Games_Simulation = 7015 + Games_Sports = 7016 + Games_Strategy = 7017 + Games_Trivia = 7018 + Games_Word = 7019 + Finance = 6015 + Entertainment = 6016 + Education = 6017 + Books = 6018 + Medical = 6020 + Newsstand = 6021 + NewsPolitics = 13001 + FashionStyle = 13002 + HomeGarden = 13003 + OutdoorsNature = 13004 + SportsLeisure = 13005 + Automotive = 13006 + ArtsPhotography = 13007 + BridesWeddings = 13008 + BusinessInvesting = 13009 + ChildrensMagazines = 13010 + ComputersInternet = 13011 + CookingFoodDrink = 13012 + CraftsHobbies = 13013 + ElectronicsAudio = 13014 + #Entertainment = 13015 + HealthMindBody = 13017 + History = 13018 + LiteraryMagazinesJournals = 13019 + MensInterest = 13020 + MoviesMusic = 13021 + ParentingFamily = 13023 + Pets = 13024 + ProfessionalTrade = 13025 + RegionalNews = 13026 + Science = 13027 + Teens = 13028 + TravelRegional = 13029 + WomensInterest = 13030 + Catalogs = 6022 + +@unique +class MacAppStoreGenre(Enum): + Business = 2001 + Developer = 12002 + Education = 12003 + Entertainment = 12004 + Finance = 12005 + Games = 12006 + Game_Action = 12201 + Game_Adventure = 12202 + Game_Arcade = 12203 + Game_Board = 12204 + Game_Card = 12205 + Game_Casino = 12206 + Game_Dice = 12207 + Game_Educational = 12208 + Game_Family = 12209 + Game_Kids = 12210 + Game_Music = 12211 + Game_Puzzle = 12212 + Game_Racing = 12213 + Game_RolePlaying = 12214 + Game_Simulation = 12215 + Game_Sports = 12216 + Game_Strategy = 12217 + Game_Trivia = 12218 + Game_Word = 12219 + Health = 2007 + Lifestyle = 12008 + Medical = 12010 + Music = 12011 + News = 12012 + Photography = 12013 + Productivity = 12014 + Reference = 12015 + Social = 12016 + Sports = 12017 + Travel = 12018 + Utilities = 12019 + Video = 12020 + Weather = 12021 + Design = 12022