-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
264 lines (218 loc) · 8.55 KB
/
start.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
import threading
import pickle
import time
from datetime import datetime
import urllib.request
from bs4 import BeautifulSoup
import re
from time import gmtime, strftime
import string
import datetime
import pymongo
import codecs
import subprocess;
from pymongo import MongoClient
import sys
starte = time.time()
list_app = []
saved = []
moya = []
moyapen = []
last_app = []
client = MongoClient('localhost', 27017)
db = client.itunes
collection = db.itunes_data
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def PagetoSoup(url): # Mise en beauté de la page
try:
response = urllib.request.urlopen( url )
except urllib.error.HTTPError as e:
print("HTTPError with: ", url, " as ", e)
return None
return BeautifulSoup(response.read())
def getApps(categoryUrl, id_itera, debugAlpha): #Récupère les applications de la page (cat/lettre)
before = []
num_page = 1 # gestion de toutes les pages
moy = 0.0
ta = time.clock()
waittime = 1
while(True):
waittime+=1
if waittime == 25:
time.sleep(5)
waittime=1
url = categoryUrl + "&page=" + str(num_page)
cat = PagetoSoup(url) # mise en forme de la page (balises etc ...)
linkss = [link.get('href') for link in cat.findAll('a', href = re.compile('^https://itunes.apple.com/us/app'))]
if linkss == before:
break
list_app.extend([tmp for tmp in linkss if tmp not in list_app])
before = linkss
num_page += 1
if id_itera == 5:
tb = time.clock()
moy = tb-ta
id_itera = 0
moyapen.append(moy)
ta = time.clock()
id_itera+=1
def Progress():
threading.Timer(5, Progress).start()
global moy
#subprocess.call(["printf", "'\033c"]);
print(bcolors.FAIL+" ____ _____ _____ ___ _____ _ _ _ _ _____ ____ ____ ___ _____ ____ _ _ ")
print(" / ___| ____|_ _| |_ _|_ _| | | | \ | | ____/ ___| | __ |_ _|_ _/ ___| | | |")
print(" | | _| _| | | | | | | | | | | \| | _| \___ \ | _ \| | | || | | |_| |")
print(" | |_| | |___ | | | | | | | |_| | |\ | |___ ___) | | |_) | | | || |___| _ |")
print(" \____|_____| |_| |___| |_| \___/|_| \_|_____|____/ |____|___| |_| \____|_| |_|"+ bcolors.ENDC)
print("")
print(" ------------------------------------------------------")
tactua = datetime.datetime.fromtimestamp(starte)
print(tactua.strftime(bcolors.OKGREEN+" - Lancé le %Y-%m-%d %H:%M:%S"))
tactua = datetime.datetime.fromtimestamp(time.time())
print(tactua.strftime(" - Nous somme le le %Y-%m-%d %H:%M:%S"))
tactua = datetime.datetime.fromtimestamp(time.time()-starte-3600)
print(tactua.strftime(" - Actif depuis %H heure(s) %M minute(s) et %S seconde(s)"))
print(" - Total: "+str(len(list_app)+len(saved)))
print(" - Save: "+ str(len(saved)))
print(" - Pending: "+ str(len(list_app)))
moyenne = 0.0
iterations = 0
for i in moya:
moyenne = (iterations*moyenne + i)/ (iterations + 1)
iterations += 1
moyenne_pen = 0.0
iterations = 0
for i in moyapen:
moyenne_pen = (iterations*moyenne_pen + i)/ (iterations + 1)
iterations += 1
print(" - Moyenne de save: "+ str(float(moyenne))+ " secondes/100")
print(" - Moyenne de pending: "+ str(float(moyenne_pen))+ " secondes/5pages"+bcolors.ENDC)
print(" ------------------------------------------------------")
def getListApp(FinalArrayCate):
id_itera = 0
for category, alphabet in [(tmp_cat, tmp_alh) for tmp_cat in FinalArrayCate for tmp_alh in string.ascii_uppercase]:
getApps(category + '&letter=' + alphabet, id_itera, None)
def insertOrNot(t):
if t != None:
document = None
posts = db.posts
for a in posts.find({'title': t['title']}):
document = 1
if document == None:
post = {"app_url": t['app_url'],
"title": t['title'],
"developer": t['developer'],
"price": t['price'],
"category": t['category'],
"lastRelease": t['lastRelease'],
"langue": t['langue'],
"compatibility": t['compatibility'],
"desc": t['desc'],
"rating": t['rating'],
"rating_reason": t['rating_reason'],
"developer_wesite": t['developer_wesite']}
posts = db.posts
post_id = posts.insert(post)
def getAppsDetails():
moy = 0.0
id_itera = 0
waittime = 1;
while(True):
waittime+=1
if waittime == 50:
time.sleep(5)
waittime=1
ta = time.clock()
for app in list_app:
t = getInfosApps(app)
insertOrNot(t)
if id_itera == 100:
tb = time.clock()
moy = tb-ta
id_itera = 0
moya.append(moy)
ta = time.clock()
id_itera+=1
def getInfosApps(appUrl):
if appUrl in saved: return None
page = PagetoSoup(appUrl)
if not page: return None
pTitleDiv = page.find( 'p', {'class' : 'title'} )
if pTitleDiv and pTitleDiv.getText() == 'One Moment Please.': return None
apps = {}
apps['app_url'] = appUrl
title = page.find('div', {'id' : 'title'})
apps['title'] = title.find('h1').getText()
apps['developer'] = title.find('h2').getText()
allDetails = page.find('div', {'id' : 'left-stack'})
if not allDetails: return None
# Price Category Updated Language Compatibility
price = allDetails.find('div', {'class' : 'price'})
if price: apps['price'] = price.getText()
else: apps['price'] = ""
category = allDetails.find('li', {'class' : 'genre'})
if category: apps['category'] = category.find('a').getText()
else: apps['category'] = ""
lastRelease = allDetails.find('li', {'class' : 'release-date'})
if lastRelease: apps['lastRelease'] = lastRelease.getText()
else: apps['lastRelease'] = ""
langue = allDetails.find('li', {'class' : 'language'})
if langue: apps['langue'] = langue.getText().split()
else: apps['langue'] = ""
compa = allDetails.find('p')
if compa: apps['compatibility'] = compa.getText()
else: apps['compatibility'] = ""
description = allDetails.find('div', {'metrics-loc' : 'Titledbox_Description'})
if description: apps['desc'] = description.getText()
else: apps['desc'] = ""
rating = allDetails.find('div', {'class' : 'app-rating'})
if rating: apps['rating'] = rating.getText()
else: apps['rating'] = ""
rating_reason = allDetails.find('ul', {'class' : 'list app-rating-reasons'})
if rating_reason: apps['rating_reason'] = [li.getText() for li in rating_reason.findAll('li')]
else: apps['rating_reason'] = ""
# Thanks anuvrat for this -> https://github.com/anuvrat/
appLinksDiv = page.find('div', {'class' : 'app-links'})
if appLinksDiv:
for link in appLinksDiv.findAll( 'a', {'class' : 'see-all'} ):
text = link.getText()
href = link.get('href')
if text.endswith('Web Site'): apps['developer_wesite'] = href
elif not text.endswith('Web Site'): apps['developer_wesite'] = ""
else:
apps['developer_wesite'] = ""
saved.append(appUrl)
list_app.remove(appUrl)
return apps
if __name__ == '__main__':
Progress()
## Page de référencement ##
allAppLinks = []
mainPage = PagetoSoup('https://itunes.apple.com/us/genre/ios/id36?mt=8')
ArrayCate = []
FinalArrayCate = []
# <div class="grid3-column">
# id="genre-nav"
# for column in ['grid3-column', 'column-']:
for x in ['list column first', 'list column', 'list column last']: # Enumeration. Page séparée en 3 column
colContent = mainPage.find('ul', {'class' : x})
ArrayCate.extend(lik.get('href') for lik in colContent.findAll('a', href = re.compile('^https://itunes.apple.com/us/genre')))
for cat in ArrayCate:
i = 0
if cat.find('photo') != -1 or cat.find('business') != -1 or cat.find('games-') != -1 or cat.find('newsstand-') != -1:
i+=1
else:
FinalArrayCate.append(cat)
# Recherche apps links
a = threading.Thread(None, getAppsDetails, None)
a.start()
# Sauvegarde
getListApp(FinalArrayCate)