-
Notifications
You must be signed in to change notification settings - Fork 1
/
leechcoin.py
executable file
·438 lines (353 loc) · 12.1 KB
/
leechcoin.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/env python
# coding=utf-8
import argparse
import urllib2 # python 2.7
import re
import sqlite3
import traceback
import datetime
import csv
headers = { 'User-Agent' : 'Mozilla/5.0 (compatible; Googlebot/2.1;'
+' +http://www.google.com/bot.html)' }
# http://www.useragentstring.com
parser = argparse.ArgumentParser()
parser.add_argument('cmd', choices=['help', 'leech', 'leechuntil', 'list', 'stats', 'search', 'searchconfig', 'config', 'check'])
parser.add_argument('-d',
help='database name to use (default:database.db)',
default='database.db')
parser.add_argument('params', nargs='*')
args = parser.parse_args()
print 'Commande :', args.cmd
print 'Args :', args
cmd = args.cmd
database = args.d
fgen = u"{0} {1:6}€ {2:3}m² {3} {4:25} {5:35} {6:2}/{7:2}/{8} {9} enligne:{13}\nhttp://www.leboncoin.fr/ventes_immobilieres/{0}.htm"
fdb = u'DB : ' + fgen
fdbs = fdb + u' {11}'
if cmd == 'help':
print 'leech [num]: download of data from page [num] (default:1)'
print 'list'
print 'stats [code postal]'
if cmd == 'list':
conn = sqlite3.connect(database)
c = conn.cursor()
for tmp in c.execute('SELECT * FROM apparts'):
print fdb.format(*tmp)
def leechpage(page, cp):
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS apparts ( '+
'id text PRIMARY KEY, ' +
'prix int, ' +
'surface int, ' +
'cp int, ville text, ' +
'nom text, ' +
'jour int, mois int, annee int, heure text, ' +
'tel text, ' +
'desc text, ' +
'siren text, ' +
'enligne int)')
req = urllib2.Request(
'http://www.leboncoin.fr/ventes_immobilieres/offres/'
#+ 'provence_alpes_cote_d_azur/bouches_du_rhone/'
+ '?'
+ 'ps=10&pe=14' # de 250k à 350k
+ '&ros=4' # pièces min
+ '&ret=1' # 1:maison, appart= '&ret=2'
#+ '&f=p' # p:particuler c:pro
+ '&location=' + cp
+ '&o=' + str(page), None, headers)
response = urllib2.urlopen(req)
re_id = re.compile('ventes_immobilieres/(?P<id>[0-9]+)\.htm')
m = re_id.finditer(response.read())
for m2 in m:
id = m2.group("id")
url = 'http://www.leboncoin.fr/ventes_immobilieres/' + id + '.htm'
c.execute('SELECT * FROM apparts WHERE id=?', (id,))
tmp = c.fetchone()
if(tmp):
try:
print fdb.format(*tmp)
except Exception:
print tmp
tmp = list(tmp)
print type(tmp[4])
tmp[4] = tmp[4].encode('utf-8')
print type(tmp[4])
print tmp
print fdb.format(*tmp)
continue
try:
response = urllib2.urlopen(url)
except Exception as e:
print 'Error on url', url
print e
continue
rep = response.read().decode('cp1252')
try:
m3 = re.findall('class="price"\>([0-9 ]+).*\<', rep)
try:
prix = int(m3[0].replace(' ', ''))
except:
print rep
m3 = re.findall(
'<th>Surface : </th>\s*<td>([0-9 ]+) m<sup>2</sup>', rep)
surface = int(m3[0].replace(' ', ''))
m3 = re.findall(
'<th>Code postal :</th>\s*<td>([0-9]+)</td>', rep)
cp = m3[0]
m3 = re.findall(
'<th>Ville :</th>\s*<td>([^<]+)</td>', rep)
try:
ville = m3[0]
except IndexError:
ville = ''
m3 = re.findall("'utilisateur_v2','N'\)\">([^<]+)</a>", rep)
nom = m3[0]
m3 = re.findall(' Mise en ligne le (\d+) (.+) à (\d+:\d+).', rep)
jour = m3[0][0]
mois = m3[0][1]
if mois[:4] == 'janv' : mois = 1
elif mois[0] == 'f' : mois = 2
elif mois[:4] == 'mars' : mois = 3
elif mois[:4] == 'avri' : mois = 4
elif mois[:3] == 'mai' : mois = 5
elif mois[:4] == 'juin' : mois = 6
elif mois[:4] == 'juil' : mois = 7
elif mois[0] == 'a' : mois = 8
elif mois[:4] == 'sept' : mois = 9
elif mois[:4] == 'octo' : mois = 10
elif mois[:3] == 'nov' : mois = 11
elif mois[0] == 'd' : mois = 12
else : mois = 0
ddt = datetime.datetime.today()
annee = ddt.year
# si on est en début d'année (ex: 3 jan 2014)
# et que l'annonce est d'un mois de fin d'année (ex: 20 déc)
# on corrige l'année (ex: pour avoir 20 déc 2013)
if ddt.month < 6 and mois > 6:
annee -= 1
heure = m3[0][2]
m3 = re.findall('/pg/0([^\.]+)\.gif" class="AdPhonenum', rep)
try:
tel_raw = m3[0]
except:
tel_raw = ''
#print m3, m3[0]
m3 = re.findall('class="content">(.+?)</div>', rep, re.DOTALL)
try:
desc = m3[0]
except:
desc = ''
m3 = re.findall('Siren : ([0-9]+)', rep)
try:
siren = m3[0]
except:
siren = 0
except Exception as e:
print 'Error on url', url
traceback.print_exc()
#print e
continue
#print ville
f = u'{0:6}€ {1:3}m² {4:4}€/m² {2:5} {3:22} {6:20} {7}/{8}/{9}@{10} {5} {12} {11}'
print f.format(
prix, surface, cp, ville, prix / surface,
url, nom, jour, mois, annee, heure, tel_raw, siren)
print desc
c.execute('INSERT INTO apparts VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', \
(id, prix, surface, cp, ville, nom, jour, mois, annee, heure, \
tel_raw, desc, siren, 1))
#exit(0)
conn.commit()
if cmd == 'leech':
try:
page = int(args.params[0])
except:
page = 1
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT * FROM config")
tmp = c.fetchone()
while(tmp):
print u'{0:10}: {1}'.format(*tmp)
if tmp[0] == 'cp' :
print 'CP:',tmp[1]
cp = tmp[1].split(',')
tmp = c.fetchone()
for cpi in cp:
print 'Leech', cpi, page
leechpage(page, cpi)
if cmd == 'leechuntil':
try:
page = int(args.params[0])
except:
page = 1
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT * FROM config")
tmp = c.fetchone()
while(tmp):
print u'{0:10}: {1}'.format(*tmp)
if tmp[0] == 'cp' :
print 'CP:',tmp[1]
cp = tmp[1].split(',')
tmp = c.fetchone()
for cpi in cp:
for i in range(page, 0, -1):
print 'Leech', cpi, i
leechpage(i,cpi)
if cmd == 'stats':
try:
cp = int(args.params[0])
except:
cp = 0
conn = sqlite3.connect(database)
c = conn.cursor()
prix_m2_cp = {}
prix_m2_cp_pro = {}
if cp:
c.execute('SELECT * FROM apparts WHERE cp=?', (cp,))
else:
c.execute('SELECT * FROM apparts')
tmp = c.fetchone()
while(tmp):
#print fdb.format(*tmp)
try:
prix_m2_cp[tmp[3]]
except:
prix_m2_cp[tmp[3]] = []
try:
prix_m2_cp_pro[tmp[3]]
except:
prix_m2_cp_pro[tmp[3]] = []
if tmp[1]/tmp[2] > 1000 and tmp[1]/tmp[2] < 8000:
if cp:
print '{0} {1:3} {2} {3:22} {4}'.format(
tmp[1], tmp[2],
str(tmp[1]/tmp[2])+'€/m²',
tmp[5].encode('utf8'),
'http://www.leboncoin.fr/ventes_immobilieres/'+tmp[0]+'.htm')
#print tmp[12]
if int(tmp[12]) > 0: #siren
prix_m2_cp_pro[tmp[3]].append(tmp[1]/tmp[2])
else: # pas pro
prix_m2_cp[tmp[3]].append(tmp[1]/tmp[2])
#print tmp[1]/tmp[2], tmp[3]
tmp = c.fetchone()
#print prix_m2_cp
#print prix_m2_cp_pro
cp_ville = {}
with open('data/insee.csv') as inseefile:
inseedata = csv.reader(inseefile, delimiter=';')
for ligne in inseedata:
try:
cp_ville[int(ligne[1])] = ligne[0]
#print int(ligne[1]), ligne[0]
except ValueError:
pass
for k in list(set(prix_m2_cp.keys() + prix_m2_cp_pro.keys())):
numpart = len(prix_m2_cp[k])
if numpart: moypart = sum(prix_m2_cp[k])/numpart
else: moypart = 0
numpro = len(prix_m2_cp_pro[k])
if numpro: moypro = sum(prix_m2_cp_pro[k])/numpro
else: moypro = 0
if moypro: ratio = moypart / float(moypro)
else: ratio = 0
print u'{0:5} part({1:3}): {2:4}€/m² pro({3:3}): {4:4}€/m² {5:3}% {6}'.format( \
k, numpart, moypart, numpro, moypro, int(ratio*100), cp_ville[k])
if cmd == 'test':
with open('data/insee.csv') as inseefile:
inseedata = csv.reader(inseefile, delimiter=';')
for ligne in inseedata:
try:
print int(ligne[1]), ligne[0]
except ValueError:
pass
if cmd == 'search':
try:
s = args.params[0]
except:
raise Exception('Recherche manquante')
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT * FROM apparts WHERE desc LIKE ?", ('%'+s+'%',))
tmp = c.fetchone()
while(tmp):
print fdbs.format(*tmp)
tmp = c.fetchone()
if cmd == 'searchconfig':
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT * FROM config")
tmp = c.fetchone()
while(tmp):
print u'{0:10}: {1}'.format(*tmp)
if tmp[0] == 'cp' :
print 'CP:',tmp[1]
cp = tmp[1].split(',')
if tmp[0] == 'prixmax' :
print 'PrixMax:',tmp[1]
prixmax = tmp[1]
if tmp[0] == 'surfmin' :
print 'SurfMin:',tmp[1]
surfmin = tmp[1]
tmp = c.fetchone()
c.execute("SELECT * FROM apparts ORDER BY annee,mois,jour,cp,heure,id")
tmp = c.fetchone()
while(tmp):
if str(tmp[3]) in cp:
print fdb.format(*tmp)
tmp = c.fetchone()
if cmd == 'config':
if len(args.params) < 1:
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT * FROM config")
print 'Config:'
tmp = c.fetchone()
while(tmp):
print u'{0:10}: {1}'.format(*tmp)
tmp = c.fetchone()
elif len(args.params) != 2:
raise Exception('Attendu: clé valeur')
else:
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS config (key text PRIMARY KEY, value text)')
try:
c.execute('INSERT INTO config VALUES (?, ?)', (args.params[0], args.params[1]))
except sqlite3.IntegrityError:
c.execute('UPDATE config SET value=? WHERE key=?', (args.params[1], args.params[0]))
conn.commit()
def check_id(id):
url = 'http://www.leboncoin.fr/ventes_immobilieres/' + id + '.htm'
try:
response = urllib2.urlopen(url)
except urllib2.HTTPError as he:
#Usually, 404
print 'HTTP Error on url', url
print he
return False
except Exception as e:
print 'Error on url', url
print e
#rep = response.read().decode('cp1252')
#if rep.find(u'Cette annonce est désactivée') > -1: return False
return True
if cmd == 'check':
conn = sqlite3.connect(database)
c = conn.cursor()
c.execute("SELECT id FROM apparts WHERE enligne=1")
tmp = c.fetchone()
dead = []
while(tmp):
print tmp
if not check_id(tmp[0]): dead.append(tmp[0])
tmp = c.fetchone()
for d in dead:
print d
c.execute("UPDATE apparts SET enligne=0 WHERE id=?", (d,))
conn.commit()
print 'Fin.'