-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp_anie.py
More file actions
87 lines (80 loc) · 2.16 KB
/
app_anie.py
File metadata and controls
87 lines (80 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
import cookielib
import mechanize
import re
def pull_html(url):
br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'csv bot v0.01')]
r = br.open(url)
data = r.read()
data = re.sub(r'\n', ' ', data)
return data
types = {
'iphone_free':[],
'iphone_paid':[],
'iphone_grossing':[],
'android_free':[],
'android_paid':[],
'android_grossing':[],
'android_top_new_free':[],
'android_top_new_paid':[]
}
data = pull_html('http://www.appannie.com/apps/ios/top/?device=iphone')
items = data.split('<tr')
for item in items:
cols = item.split('main-info')
slot = 0
for col in cols:
m = re.search(r'^"><span title="([^"]+)"', col)
if m:
name = m.group(1).strip()
if slot == 0:
types['iphone_free'].append(name)
if slot == 1:
types['iphone_paid'].append(name)
if slot == 2:
types['iphone_grossing'].append(name)
slot += 1
data = pull_html('http://www.appannie.com/apps/google-play/top/united-states')
items = data.split('<tr')
for item in items:
cols = item.split('main-info')
slot = 0
for col in cols:
m = re.search(r'^"><span title="([^"]+)"', col)
if m:
name = m.group(1).strip()
if slot == 0:
types['android_free'].append(name)
if slot == 1:
types['android_paid'].append(name)
if slot == 2:
types['android_grossing'].append(name)
if slot == 3:
types['android_top_new_free'].append(name)
if slot == 4:
types['android_top_new_paid'].append(name)
slot += 1
# now just need to create csv
lines = []
for key in types.keys():
line_count = 0
if len(lines) - 1 > 0:
lines[0].append('"%s"' % key)
else:
lines.append(['0', '"%s"' % key])
for item in types[key]:
line_count += 1
try:
# already have this line; just append to it
lines[line_count].append('"%s"' % item)
except:
# don't have this line so need to set it up
lines.append(['%s' % line_count, '"%s"' % item])
f = open('app_sales_data.csv', 'w')
for line in lines:
f.write(', '.join(line))
f.write('\n')