Skip to content

Commit 1e68911

Browse files
committed
Code refactor
1 parent 989a33d commit 1e68911

File tree

6 files changed

+189
-252
lines changed

6 files changed

+189
-252
lines changed

src/genres/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/genres/mac.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/iosr3.py renamed to src/ios-ranking.py

Lines changed: 11 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22
# -*- coding: utf-8 -*-
33

44
import sys
5-
import json
6-
import http.client
7-
import datetime
85
import argparse
96

10-
from genres import ios
11-
from util import countries, platforms
12-
13-
store_server = str("itunes.apple.com")
7+
from util import countries, platforms, genres, common
148

159

1610
def ranking_free(iso_code, platform, genre):
@@ -26,9 +20,9 @@ def ranking_free(iso_code, platform, genre):
2620
else:
2721
store_section = "FreeAppleTVApps"
2822

23+
store_uri = common.make_store_uri(iso_code, genre, store_section)
2924

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

3327

3428
def ranking_paid(iso_code, platform, genre):
@@ -42,9 +36,9 @@ def ranking_paid(iso_code, platform, genre):
4236
else:
4337
store_section = "PaidAppleTVApps"
4438

45-
store_uri = "/WebObjects/MZStoreServices.woa/ws/charts?cc={0}&g={1}&name={2}&limit=400".format(iso_code, genre, store_section)
46-
47-
return request_url(store_uri)
39+
store_uri = common.make_store_uri(iso_code, genre, store_section)
40+
41+
return common.fetch(store_uri, AppID)
4842

4943

5044
def ranking_grossing(iso_code, platform, genre):
@@ -58,43 +52,9 @@ def ranking_grossing(iso_code, platform, genre):
5852
else:
5953
store_section = "AppleTVAppsByRevenue"
6054

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

63-
return request_url(store_uri)
64-
65-
66-
def request_url(url):
67-
"""
68-
Request an URL
69-
"""
70-
conn = http.client.HTTPSConnection(store_server)
71-
headers = { "Cache-Control" : "no-cache" }
72-
conn.request("GET", url, None, headers)
73-
r1 = conn.getresponse()
74-
75-
data1 = r1.read()
76-
77-
ranking = json.loads(data1)
78-
79-
conn.close()
80-
81-
# JSON document
82-
posicion = process_result(ranking)
83-
84-
return posicion
85-
86-
87-
def process_result(result):
88-
"""
89-
90-
"""
91-
entries = result["resultIds"]
92-
93-
try:
94-
position = entries.index(AppID)
95-
return (position + 1)
96-
except ValueError:
97-
return "---"
57+
return common.fetch(store_uri, AppID)
9858

9959

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

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

10969

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

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

12181
#
12282
# Workflow
@@ -159,7 +119,7 @@ def print_parameters():
159119
# App category
160120

161121
try:
162-
genre = ios.AppStoreGenre(args.category)
122+
genre = genres.AppStoreGenre(args.category)
163123
except ValueError:
164124
print_parameters()
165125
print("\tCategory not available. Take a look at category section above.\r\n")

src/macr3.py renamed to src/mac-ranking.py

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,19 @@
22
# -*- coding: utf-8 -*-
33

44
import sys
5-
import json
6-
import http.client
7-
import datetime
85
import 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

8120
def 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("\tExample: macr.py -appid 123456789 -category 12014 -stores top\r\n")
34+
print("\tExample: 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
13574
try:
136-
genre = mac.MacAppStoreGenre(args.category)
75+
genre = genres.MacAppStoreGenre(args.category)
13776
except ValueError:
13877
print_parameters()
13978
print("\tCategory not available. Take a look at category section above.\r\n")
@@ -148,11 +87,11 @@ def print_parameters():
14887
print("\t{0:20}{1:>10}{2:>10}{3:>10}\r\n".format("Country", "Free", "Paid", "Grossing"))
14988

15089
for 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

15796
print("\r\n\t{0:^50}".format("A script by @fitomad."))
15897
print("\t{0:^50}\r\n".format("GitHub: https://github.com/fitomad/App-Store-Ranking"))

src/util/common.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import http.client
2+
import json
3+
4+
store_server = str("itunes.apple.com")
5+
6+
def make_store_uri(country_code: str, genre: str, section: str) -> str:
7+
"""
8+
9+
"""
10+
store_uri = f"/WebObjects/MZStoreServices.woa/ws/charts?cc={country_code}&g={genre}&name={section}&limit=400"
11+
12+
return store_uri
13+
14+
15+
def fetch(url, appID):
16+
"""
17+
Request an URL
18+
"""
19+
conn = http.client.HTTPSConnection(store_server)
20+
headers = { "Cache-Control" : "no-cache" }
21+
conn.request("GET", url, None, headers)
22+
response = conn.getresponse()
23+
24+
data = response.read()
25+
26+
ranking = json.loads(data)
27+
28+
conn.close()
29+
30+
# JSON document
31+
posicion = process_result(ranking, appID)
32+
33+
return posicion
34+
35+
36+
def process_result(result, appID):
37+
"""
38+
Process JSON and recover results
39+
"""
40+
entries = result["resultIds"]
41+
42+
try:
43+
position = entries.index(appID)
44+
return (position + 1)
45+
except ValueError:
46+
return "---"

0 commit comments

Comments
 (0)