-
Notifications
You must be signed in to change notification settings - Fork 12
/
utilities.py
616 lines (524 loc) · 20.3 KB
/
utilities.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__authors__ = [
'Yoann Sculo <[email protected]>',
'Bruno Adelé <[email protected]>',
]
__copyright__ = 'Copyright (C) 2013 Yoann Sculo'
__license__ = 'GPLv2'
__version__ = '1.0'
# System
import os
import re
import time
import glob
import hashlib
import fnmatch
import importlib
import html2text
import sqlite3 as lite
import requests
# Class for terminal Color
class tcolor:
DEFAULT = "\033[0m"
BOLD = "\033[1m"
RED = "\033[91m"
GREEN = "\033[92m"
BLUE = "\033[96m"
ORANGE = "\033[93m"
MAGENTA = "\033[95m"
RESET = "\033[2J\033[H"
BELL = "\a"
from collections import namedtuple
PageResult = namedtuple(
'PageResult',
[
'version',
'statuscode',
'pageid',
'url',
'content'
]
)
DownloadResult = namedtuple('DownloadResult', ['url', 'statuscode', 'content'])
PAGEVERSION = '1.0'
def showMessage(text, level="info", section=""):
messcolor = tcolor.GREEN
if level.lower() == "error":
messcolor = tcolor.RED
elif level.lower() == "warn":
messcolor = tcolor.ORANGE
if section == "":
title = tcolor.DEFAULT
else:
title = "%s[%s%s%s] " % (
tcolor.DEFAULT,
messcolor,
section,
tcolor.DEFAULT
)
print "%s%s" % (
title,
text
)
def getEncodedURL(url, datas):
datas = None
if datas:
urlid = "%s/%s" % (url, datas)
else:
urlid = "%s" % url
urlid = "%s/%s" % (url, datas)
pageid = md5(urlid)
return pageid
def getFeedDestination(rootdir, jobboardname, url, datas):
feedid = getEncodedURL(url, datas)
saveto = "%s/%s/feeds/%s.feed" % (
rootdir,
jobboardname,
feedid
)
return saveto
def getPageDestination(rootdir, jobboardname, feedid, url, datas):
pageid = getEncodedURL(url, datas)
saveto = "%s/%s/pages/%s/%s.page" % (
rootdir,
jobboardname,
feedid,
pageid,
)
return saveto
def htmltotext(text):
"""Fix html2text"""
html2text.BODY_WIDTH = 0
return html2text.html2text(text)
def md5(datas):
"""Calc md5sum string datas"""
return hashlib.md5(datas).hexdigest()
def getModificationFile(filename):
""" Get a modification file (in timestamp)"""
t = None
try:
t = os.path.getmtime(filename)
except:
pass
return t
def findFiles(srcdir, pattern):
""" Find files with the pattern"""
matches = []
for root, dirnames, filenames in os.walk(srcdir):
for filename in fnmatch.filter(filenames, pattern):
matches.append(os.path.join(root, filename))
return matches
def getNow():
"""Get now timestamp"""
return time.time()
def openPage(filename):
pageresult = None
fd = open(filename, 'rb')
version = fd.readline().strip()
statuscode = fd.readline().strip()
pageid = fd.readline().strip()
url = fd.readline().strip()
content = fd.read()
fd.close()
if version != PAGEVERSION:
mess = "The %s has bad version, current=%s, needed=%s" % \
(
filename,
version,
PAGEVERSION
)
showMessage(mess, "warn", "Utilities")
else:
pageresult = PageResult(
version=version,
statuscode=statuscode,
pageid=pageid,
url=url,
content=content
)
return pageresult
def download(url, datas):
headers = {
'User-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',
'Referer': 'http://candidat.pole-emploi.fr/candidat/rechercheoffres/avancee',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#'Content-Type': 'application/json',
}
try:
if datas:
r = requests.post(url, data=datas, headers=headers)
else:
r = requests.get(url)
except Exception, e:
showMessage(e, 'error', 'Utilities')
return DownloadResult(url=url, statuscode=r.status_code, content=r.content)
def downloadFile(
filename, url, datas=None, withmeta=False,
age=0, forcedownload=False):
headers = {
'User-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',
'Referer': 'http://candidat.pole-emploi.fr/candidat/rechercheoffres/avancee',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#'Content-Type': 'application/json',
}
# Check if i must download file
if os.path.isfile(filename):
now = getNow()
t = getModificationFile(filename)
if not forcedownload and t + (age) > now:
return
destdir = os.path.dirname(filename)
if (not os.path.isdir(destdir)):
os.makedirs(destdir)
# Download file
showMessage("Download %s(%s%s%s)" %
(
url,
tcolor.MAGENTA,
filename,
tcolor.DEFAULT
),
'info',
'Utilities',
)
r = download(url, datas)
if r.statuscode in [200, 404, 410]:
out = open(filename, 'wb')
if withmeta:
version = PAGEVERSION
statuscode = r.statuscode
pageid = getEncodedURL(url, datas)
out.write("%s\n" % version)
out.write("%s\n" % statuscode)
out.write("%s\n" % pageid)
out.write("%s\n" % url)
out.write(r.content)
out.close()
return r
def removeFiles(rep, patern):
filelist = glob.glob("%s/%s" % (rep, patern))
for f in filelist:
os.remove(f)
def loadJobBoard(jobboardname, configs):
"""Load Jobboard plugin"""
module = importlib.import_module(
'jobboards.%s' % jobboardname
)
moduleClass = getattr(module, "JB%s" % jobboardname)
return moduleClass(configs)
def db_checkandcreate(configs):
"""Check and create offers table"""
if not db_istableexists(configs, 'offers'):
db_create(configs)
def db_istableexists(configs, tablename):
"""Check if tablename exist"""
conn = lite.connect(configs.globals['database'])
cursor = conn.cursor()
sql = "SELECT name FROM sqlite_master WHERE type='table' AND name='%s';" % tablename
cursor.execute(sql)
return len(cursor.fetchall()) == 1
def db_create(configs):
"""Create the offers table"""
conn = None
conn = lite.connect(configs.globals['database'])
cursor = conn.cursor()
# create a offers table
cursor.execute("""CREATE TABLE offers( \
source TEXT, \
offerid TEXT, \
lastupdate INTEGER, \
ref TEXT, \
feedid TEXT, \
date_pub INTEGER, \
date_add INTEGER, \
title TEXT, \
company TEXT, \
contract TEXT, \
duration INTEGER, \
location TEXT, \
department TEXT, \
lat TEXT, \
lon TEXT, \
salary TEXT, \
salary_cleaned TEXT, \
salary_min FLOAT, \
salary_max FLOAT, \
salary_nbperiod INTEGER, \
salary_unit FLOAT, \
salary_bonus TEXT, \
salary_minbonus FLOAT, \
salary_maxbonus FLOAT, \
url TEXT, \
content TEXT, \
state TEXT, \
PRIMARY KEY(source, offerid))""")
# create a offers table
# cursor.execute("""CREATE TABLE feeds( \
# feedid TEXT, \
# ref TEXT, \
# PRIMARY KEY(feedid, ref))""")
cursor.execute("""CREATE TABLE blacklist(company TEXT, PRIMARY KEY(company))""")
def db_delete_jobboard_datas(configs, jobboardname):
"""Delete jobboard datas from offers table"""
conn = None
conn = lite.connect(configs.globals['database'])
cursor = conn.cursor()
# Check if must delete all offers
filter = ''
if jobboardname != 'ALL':
filter = "where source='%s'" % jobboardname
# Delete jobbord datas in offers
sql = "delete from offers %s" % filter
cursor.execute(sql)
# # Delete jobbor datas on jobboard table
# sql = "delete from jb_%s" % jobboardname
# cursor.execute(sql)
conn.commit()
def db_delete_offer(configs, source, offerid):
"""Delete offer"""
conn = None
conn = lite.connect(configs.globals['database'])
cursor = conn.cursor()
# create a table
sql = "delete from offers where source='%s' and offerid='%s'" % \
(
source,
offerid
)
cursor.execute(sql)
conn.commit()
def db_add_offer(configs, offer):
conn = lite.connect(configs.globals['database'])
try:
conn.text_factory = str
cursor = conn.cursor()
cursor.execute(
"INSERT INTO offers VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
(
offer.src, offer.offerid, offer.lastupdate, offer.ref,
offer.feedid, offer.date_pub, offer.date_add,
offer.title, offer.company, offer.contract,
offer.duration, offer.location, offer.department,
offer.lat, offer.lon, offer.salary, offer.salary_cleaned,
offer.salary_min, offer.salary_max, offer.salary_nbperiod,
offer.salary_unit, offer.salary_bonus, offer.salary_minbonus,
offer.salary_maxbonus, offer.url, offer.content, offer.state
)
)
conn.commit()
return 0
except lite.Error, e:
""" TODO : Waiting for this ... http://bugs.python.org/issue16379
lite.errorcode.get(e.sqlite_errorcode)
USE pip
In the meantime, let's do something realy dirty. let's catch errors
by string ! ugh
"""
if (e.args[0] == "columns source, offerid are not unique"):
return 0
else:
showMessage(e, 'error', 'Utilities')
return 1
finally:
if conn:
conn.close()
def blacklist_flush(configs):
conn = lite.connect(configs.globals['database'])
cursor = conn.cursor()
sql = "DELETE FROM blacklist"
cursor.execute(sql)
conn.commit()
conn.close()
def blocklist_load(configs):
fp = open('blacklist_company.txt', 'r')
list = []
for line in fp:
company = unicode(line.rstrip('\n'))
list.append([company])
fp.close()
conn = None
try:
conn = lite.connect(configs.globals['database'])
conn.text_factory = str
cursor = conn.cursor()
cursor.executemany("INSERT INTO blacklist VALUES(?)", list)
conn.commit()
except lite.Error, e:
showMessage("Error %s:" % e.args[0], 'error', 'Utilities')
finally:
if conn:
conn.close()
def filter_contract_fr(contract):
contract = re.sub(ur'Perm', "CDI", contract)
contract = re.sub(ur'[0-9]+ en (.*)', "\\1", contract, flags=re.DOTALL)
contract = re.sub(ur'.*CDI.*', "CDI", contract, flags=re.DOTALL)
contract = re.sub(ur'Travail temporaire', "CDD", contract, flags=re.DOTALL)
contract = re.sub(
ur'.*CDD.*de (.*)',
"CDD de \\1",
contract, flags=re.DOTALL
)
# PoleEmploi
contract = re.sub(ur'.*Contrat à durée indéterminée.*', "CDI", contract, flags=re.DOTALL)
contract = re.sub(ur'.*Contrat à durée déterminée de.*', "CDD", contract, flags=re.DOTALL)
contract = re.sub(ur'.*Contrat travail saisonnier de.*', "CDD", contract, flags=re.DOTALL)
return contract
def filter_company_fr(company):
company = re.sub(ur'^\(confidentiel\)$', 'NA', company, flags=re.IGNORECASE)
company = re.sub(ur'^confidentiel$', 'NA', company, flags=re.IGNORECASE)
return company
def filter_location_fr(location):
location = re.sub(ur'^IDF$', "Île-de-France", location, flags=re.IGNORECASE)
location = re.sub(ur'^Ile-de-France$', "Île-de-France", location, flags=re.IGNORECASE)
location = re.sub(ur'^Ile de France$', "Île-de-France", location, flags=re.IGNORECASE)
location = re.sub(ur'^Region Parisienne$', "Île-de-France", location, flags=re.IGNORECASE)
location = re.sub(ur'^PARIS$', "Paris", location, flags=re.IGNORECASE)
return location
def filter_salary_fr(salary):
# TODO : use regexp once whe have a better view of possible combinations
# TODO : use something similar as ^...$
flags = re.DOTALL | re.IGNORECASE
men_salary = salary
salary = re.sub(ur'selon profil', '', salary, flags=flags)
salary = re.sub(ur"Selon (l')?exp.?rien.?e", '', salary, flags=flags)
salary = re.sub(ur'.? n.?gocier', '', salary, flags=flags)
salary = re.sub(ur'.? d.?finir', '', salary, flags=flags)
salary = re.sub(ur'Non pr.?cis.?', '', salary, flags=flags)
salary = re.sub(ur'Selon dipl.?me', '', salary, flags=flags)
salary = re.sub(ur'et exp.?rience', '', salary, flags=flags)
#salary = re.sub(ur'fixe + variable selon profil', "NA", salary)
#salary = re.sub(ur'Fixe+Variable selon profil', "NA", salary)
#salary = re.sub(ur'Fixe + Variable selon profil', "NA", salary)
#salary = re.sub(ur'A définir selon profil', "NA", salary)
#salary = re.sub(ur'A DEFINIR SELON PROFIL', "NA", salary)
#salary = re.sub(ur'à défninir selon profil', "NA", salary)
#salary = re.sub(ur'à définir selon profils', "NA", salary)
#salary = re.sub(ur'A négocier selon profil', "NA", salary)
#salary = re.sub(ur'A NEGOCIER SELON PROFIL', "NA", salary)
#salary = re.sub(ur'à négocier selon profil', "NA", salary)
#salary = re.sub(ur'à négocier selon le profil', "NA", salary)
#salary = re.sub(ur'à déterminer selon profil', "NA", salary)
salary = re.sub(ur'à définir selon expérience', "NA", salary)
salary = re.sub(ur'à négocier selon exp', "NA", salary)
salary = re.sub(ur'à negocier K€ brut/an', "NA", salary)
#salary = re.sub(ur'A voir selon profil', "NA", salary)
#salary = re.sub(ur'Attractive et selon profil', "NA", salary)
salary = re.sub(ur'De débutant à confirmé', "NA", salary)
salary = re.sub(ur'En fonction du profil', "NA", salary)
salary = re.sub(ur'en fonction du profil', "NA", salary)
salary = re.sub(ur'En fonction de votre profil', "NA", salary)
salary = re.sub(ur'Fonction profil et expérience', "NA", salary)
#salary = re.sub(ur'Selon profil/expérience', "NA", salary)
salary = re.sub(ur'Package Attractif selon expéri', "NA", salary)
salary = re.sub(ur'selon niveau d\'expérience', "NA", salary)
salary = re.sub(ur'Selon formation et/ou exp.', "NA", salary)
salary = re.sub(ur'selon votre profil', "NA", salary)
salary = re.sub(ur'Selon votre profil.', "NA", salary)
salary = re.sub(ur'Selon votre profil', "NA", salary)
salary = re.sub(ur'selon le profil', "NA", salary)
salary = re.sub(ur'Selon le profil', "NA", salary)
salary = re.sub(ur'selo profil', "NA", salary)
salary = re.sub(ur'Suivant profil', "NA", salary)
salary = re.sub(ur'selon l\'expérience', "NA", salary)
salary = re.sub(ur'selon experience', "NA", salary)
salary = re.sub(ur'selon expérience', "NA", salary)
salary = re.sub(ur'Selon expérience', "NA", salary)
salary = re.sub(ur'Selon expérince', "NA", salary)
salary = re.sub(ur'Selon Expérience', "NA", salary)
salary = re.sub(ur'Selon experience', "NA", salary)
salary = re.sub(ur'SELON EXPERIENCE', "NA", salary)
salary = re.sub(ur'Selon compétences', "NA", salary)
salary = re.sub(ur'Selon compétence', "NA", salary)
salary = re.sub(ur'selon exp.', "NA", salary)
salary = re.sub(ur'Selon exp.', "NA", salary)
salary = re.sub(ur'selon exp', "NA", salary)
salary = re.sub(ur'SELON EXP', "NA", salary)
salary = re.sub(ur'Selon CCN 15/3/66', "NA", salary)
salary = re.sub(ur'selon CCN51', "NA", salary)
salary = re.sub(ur'selon grille FPH', "NA", salary)
salary = re.sub(ur'selon grille', "NA", salary)
salary = re.sub(ur'selon grilles', "NA", salary)
salary = re.sub(ur'cf. annonce', "NA", salary)
salary = re.sub(ur'Package attractif', "NA", salary)
salary = re.sub(ur'suivant profil', "NA", salary)
salary = re.sub(ur'fixe + variable', "NA", salary)
salary = re.sub(ur'Fixe + variable', "NA", salary)
salary = re.sub(ur'grille fonction publique', "NA", salary)
salary = re.sub(ur'Contrat Apprentissage', "NA", salary)
salary = re.sub(ur'Salaire Attractif', "NA", salary)
salary = re.sub(ur'Salaire à négocier', "NA", salary)
salary = re.sub(ur'à déterminer', "NA", salary)
salary = re.sub(ur'A déterminer', "NA", salary)
salary = re.sub(ur'à convenir', "NA", salary)
salary = re.sub(ur'A convenir', "NA", salary)
salary = re.sub(ur'à débattre', "NA", salary)
salary = re.sub(ur'à négocier', "NA", salary)
salary = re.sub(ur'a négocier', "NA", salary)
salary = re.sub(ur'a negocier', "NA", salary)
salary = re.sub(ur'à negocier', "NA", salary)
salary = re.sub(ur'A négocier.', "NA", salary)
salary = re.sub(ur'A négocier', "NA", salary)
salary = re.sub(ur'A NEGOCIER', "NA", salary)
salary = re.sub(ur'A negocier', "NA", salary)
salary = re.sub(ur'a confirmer', "NA", salary)
salary = re.sub(ur'à définir', "NA", salary)
salary = re.sub(ur'à défiinir', "NA", salary)
salary = re.sub(ur'À définir', "NA", salary)
salary = re.sub(ur'A définir.', "NA", salary)
salary = re.sub(ur'A définir', "NA", salary)
salary = re.sub(ur'A Definir', "NA", salary)
salary = re.sub(ur'A DEFINIR', "NA", salary)
salary = re.sub(ur'A DETERMINER', "NA", salary)
salary = re.sub(ur'à determiner', "NA", salary)
salary = re.sub(ur'A determiner', "NA", salary)
salary = re.sub(ur'A definir', "NA", salary)
salary = re.sub(ur'À definir', "NA", salary)
salary = re.sub(ur'A défnir', "NA", salary)
salary = re.sub(ur'A defnir', "NA", salary)
salary = re.sub(ur'A discuter', "NA", salary)
salary = re.sub(ur'Attractif', "NA", salary)
salary = re.sub(ur'à préciser', "NA", salary)
salary = re.sub(ur'en fonction exp.', "NA", salary)
salary = re.sub(ur'Negotiable', "NA", salary)
salary = re.sub(ur'negotiable', "NA", salary)
salary = re.sub(ur'Négociable', "NA", salary)
salary = re.sub(ur'négociable', "NA", salary)
salary = re.sub(ur'negociable', "NA", salary)
salary = re.sub(ur'NEGOCIABLE', "NA", salary)
salary = re.sub(ur'Negociable', "NA", salary)
salary = re.sub(ur'non indiqué', "NA", salary)
salary = re.sub(ur'non communiqué', "NA", salary)
salary = re.sub(ur'NON COMMUNIQUE', "NA", salary)
salary = re.sub(ur'non précisé', "NA", salary)
salary = re.sub(ur'Non précisé.', "NA", salary)
salary = re.sub(ur'Non précisé', "NA", salary)
salary = re.sub(ur'Motivant.', "NA", salary)
salary = re.sub(ur'Motivant', "NA", salary)
salary = re.sub(ur'Voir annonce', "NA", salary)
salary = re.sub(ur'GRILLE DE LA FPT', "NA", salary)
salary = re.sub(ur'Grille', "NA", salary)
salary = re.sub(ur'variable', "NA", salary)
salary = re.sub(ur'confidentiel', "NA", salary)
salary = re.sub(ur'TBD', "NA", salary)
salary = re.sub(ur'N.S.', "NA", salary)
salary = re.sub(ur'ANNUEL', "NA", salary)
salary = re.sub(ur'^euros$', "NA", salary)
salary = re.sub(ur'NC K€ brut/an', "NA", salary)
salary = re.sub(ur'xx K€ brut/an', "NA", salary)
# salary = re.sub(ur'0K€ brut/an', "NA", salary)
salary = re.sub(ur'--K€ brut/an', "NA", salary)
salary = re.sub(ur'- K€ brut/an', "NA", salary)
salary = re.sub(ur'-K€ brut/an', "NA", salary)
salary = re.sub(ur'XK€ brut/an', "NA", salary)
salary = re.sub(ur'N/C', "NA", salary)
salary = re.sub(ur'N.C', "NA", salary)
salary = re.sub(ur'NC', "NA", salary)
salary = re.sub(ur'nc', "NA", salary)
# Final clean
salary = re.sub(ur'\.$', "", salary)
if salary.strip() == '':
salary = "NA"
else:
salary = men_salary
return salary