22# -*- coding: utf-8 -*-
33
44import sys
5- import json
6- import http .client
7- import datetime
85import argparse
96
10- from genres import mac
11- from util import countries
7+ from util import countries , genres , common
128
13- store_server = str ("itunes.apple.com" )
149
15- def make_store_uri (country_code : str , genre : str , section : str ) -> str :
16- store_uri = f"/WebObjects/MZStoreServices.woa/ws/charts?cc={ country_code } &g={ genre } &name={ section } &limit=400"
17-
18- return store_uri
19-
20-
21-
22- def ranking_free (iso_code ):
23- """
24- Top free Mac app ranking
25- """
26- store_uri = make_store_uri (iso_code , str (genre .value ), "FreeMacApps" )
27-
28- return request_url (store_uri )
29-
30-
31- def ranking_paid (iso_code ):
32- """
33- Top paid Mac applications
34- """
35- store_uri = make_store_uri (iso_code , str (genre .value ), "PaidMacApps" )
36-
37- return request_url (store_uri )
38-
39-
40- def ranking_grossing (iso_code ):
10+ def ranking (iso_code , section ):
4111 """
42- Top grossing Mac applications
12+ Mac App Store ranking for a country and a section
4313 """
44- store_uri = make_store_uri (iso_code , str (genre .value ), "MacAppsByRevenue" )
45-
46- return request_url (store_uri )
47-
48- """
49- Request an URL
50- """
51- def request_url (url ):
52- conn = http .client .HTTPSConnection (store_server )
53- headers = { "Cache-Control" : "no-cache" }
54- conn .request ("GET" , url , None , headers )
55- r1 = conn .getresponse ()
56-
57- data1 = r1 .read ()
58-
59- ranking = json .loads (data1 )
60-
61- conn .close ()
62-
63- # JSON document
64- posicion = process_result (ranking )
65-
66- return posicion
67-
68- """
69- Process JSON and recover results
70- """
71- def process_result (result ):
72- entries = result ["resultIds" ]
14+ store_uri = common .make_store_uri (iso_code , str (genre .value ), section )
15+ position = common .fetch (store_uri , AppID )
7316
74- try :
75- position = entries .index (AppID )
76- return (position + 1 )
77- except ValueError :
78- return "---"
17+ return position
7918
8019
8120def print_parameters ():
@@ -84,15 +23,15 @@ def print_parameters():
8423 """
8524 print ("\r \n \t {0:^26}\r \n " .format ("Mac App Store Categories" ))
8625
87- for genre in mac .MacAppStoreGenre :
26+ for genre in genres .MacAppStoreGenre :
8827 print ("\t {0:^20} = {1:<6}" .format (genre .name , genre .value ))
8928
9029
9130 print ("\r \n \t {0:^26}\r \n " .format ("Countries availables" ))
9231 print ("\t {0:^20} = {1:<6}" .format ("All countries" , "world" ))
9332 print ("\t {0:^20} = {1:<6}\r \n " .format ("Top 10 (by revenue)" , "top" ))
9433
95- print ("\t Example: macr .py -appid 123456789 -category 12014 -stores top\r \n " )
34+ print ("\t Example: mac-ranking .py -appid 123456789 -category 12014 -stores top\r \n " )
9635
9736#
9837# Workflow.
@@ -133,7 +72,7 @@ def print_parameters():
13372
13473# App category
13574try :
136- genre = mac .MacAppStoreGenre (args .category )
75+ genre = genres .MacAppStoreGenre (args .category )
13776except ValueError :
13877 print_parameters ()
13978 print ("\t Category not available. Take a look at category section above.\r \n " )
@@ -148,11 +87,11 @@ def print_parameters():
14887print ("\t {0:20}{1:>10}{2:>10}{3:>10}\r \n " .format ("Country" , "Free" , "Paid" , "Grossing" ))
14988
15089for country in store_countries :
151- posicion_free = ranking_free (country [1 ])
152- posicion_paid = ranking_paid (country [1 ])
153- posicion_grossing = ranking_grossing (country [1 ])
90+ position_free = ranking (country [1 ], "FreeMacApps" )
91+ position_paid = ranking (country [1 ], "PaidMacApps" )
92+ position_grossing = ranking (country [1 ], "MacAppsByRevenue" )
15493
155- print ("\t {0:20.17}{1:>10}{2:>10}{3:>10}" .format (country [2 ], posicion_free , posicion_paid , posicion_grossing ))
94+ print ("\t {0:20.17}{1:>10}{2:>10}{3:>10}" .format (country [2 ], position_free , position_paid , position_grossing ))
15695
15796print ("\r \n \t {0:^50}" .format ("A script by @fitomad." ))
15897print ("\t {0:^50}\r \n " .format ("GitHub: https://github.com/fitomad/App-Store-Ranking" ))
0 commit comments