-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathensap.rb
190 lines (162 loc) · 4.83 KB
/
ensap.rb
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
#!/usr/bin/ruby
# Marc Quinton / janvier 2019 - client CLI sur le site de l'ENSAP ; version 0.12
# URL du service : https://ensap.gouv.fr/
#
# cette application permet de recupérer vos fiches de paye numérisée sur le site de l'ENSAP
# - les fiches de paye sont disponibles depuis l'année 2016 jusqu'à l'année en cours
# - elles sont téléchargées localement dans le dossier "docs"
# - vous devez configurer le login et MDP ; le login est votre numéro de sécurité sociale en 15 chiffres
# - le fichier PDF ne sera pas téléchargé s'il est disponible localement. Pas de vérification d'intégrité sur le contenu
#
# dependances : gem json, mechanize, (pp)
# commande d'installation : gem install pp json mechanize
# mots-clés ; keywords : ENSAP, PDF, API, script, CLI, github, download, téléchargement, fiche de paie, dématérialisation, pay sheet, fonction publique.
# links :
# - https://github.com/deep75/ENSAP-Mobile (android app)
require 'mechanize'
require 'pp'
require 'json'
class Ensap
def initialize user, passwd
@user=user
@passwd=passwd
@client = Mechanize.new
@cache={
:years => nil
}
@url={
:home => 'https://ensap.gouv.fr/web/accueil',
:login => 'https://ensap.gouv.fr/authentification',
# habilitations / droits d'accès
:acl => 'https://ensap.gouv.fr/prive/initialiserhabilitation/v1',
# concerne les rémunérations
# donne la liste des documents accessibles au téléchargement par année.
:remuneration => 'https://ensap.gouv.fr/prive/anneeremuneration/v1/', # remuneration by year
:download => 'https://ensap.gouv.fr/prive/telechargerdocumentremuneration/v1',
# quelles sont les années disponibles à la lecture, téléchargement
:years => 'https://ensap.gouv.fr/prive/listeanneeremuneration/v1'
}
end
def login
data = {
'identifiant' => @user,
'secret' => @passwd
}
[email protected](@url[:login], data)
if res.body.match(/Authentification OK/)
@client.get @url[:home]
return true
else
return false
end
end
# get some ACL and account status from portal
#
# {"lectureSeule"=>false,
# "listeService"=>
# {"compteindividuelretraite"=>true,
# "demandedepartretraite"=>true,
# "pensionne"=>false,
# "remuneration"=>true,
# "retraite"=>true,
# "simulation"=>false,
# "suividepartretraite"=>false}}
def acl
headers = { 'Content-Type' => 'application/json; charset=utf-8'}
args={}
JSON.parse(@client.post(@url[:acl], args, headers).body)
end
def years
@cache[:years]=JSON.parse(@client.get(@url[:years]).body)['donnee'] unless @cache[:years]!=nil
@cache[:years]
end
# return an array of hash :
# {"documentUuid"=>"b0ba4820-XXXX-YYYY-bf90-06b560530509",
# "libelle1"=>"Janvier 2018",
# "libelle2"=>"2018_01_BP_janvier.pdf (PDF, 20 Ko)",
# "nomDocument"=>"2018_01_BP_janvier.pdf",
# "dateDocument"=>"2018-01-01T12:00:00.000+0100",
# "annee"=>2018,
# "icone"=>"document",
# "libelleIcone"=>"Icône bulletin de paye"},
def remuneration_by_year year
puts "remuneration_by_year(#{year})"
if year==:all
list=[]
self.years.each do |y|
list.concat(self.remuneration_by_year(y))
end
return list.sort_by{ |e| e['dateDocument']}
else
JSON.parse(@client.get(@url[:remuneration]+year.to_s).body)['donnee']
end
end
alias ls remuneration_by_year
# https://ensap.gouv.fr/prive/telechargerdocumentremuneration/v1?documentUuid=XYZ-XXX-123
def download id
args={
:documentUuid => id
}
[email protected](@url[:download], args)
return nil unless data.code=="200"
return data.body
end
def download_by_year year, dir
puts "download_by_year(#{dir})"
if year==:all
self.years.each do |y|
self.download_by_year y, dir
end
else
Dir.mkdir dir unless Dir.exist? dir
self.remuneration_by_year(year).each do |_doc|
file='docs/'+_doc['nomDocument']
unless File.exist? file
puts "# downloading " + _doc['nomDocument']
data=self.download _doc['documentUuid']
IO.write(file, data) unless data==nil
end
end
end
end
def dl_all dir='docs/'
self.years.each do |y|
self.dl y, dir
end
end
end
if ARGV.size>=2
client=Ensap.new ARGV[0], ARGV[1]
else
# you can modify user and password here.
client=Ensap.new '123456789012345', 'your-password'
end
if ARGV.size==3
cmd=ARGV[2]
else
cmd=:ls
end
docs='./docs'
if client.login
puts "# connexion réussie"
case cmd.to_sym
when :test
pp client.years
when :acl
pp client.acl
when :ls
year=:all if ARGV.size==3
year=ARGV[3].to_sym if ARGV.size==4
pp client.ls year
# dowload all documents to localdir
when :dl|:download
# pour les années de 2016 à l'année courante :
client.dl :all, docs
# download current year
when :dl_current
# pour les années de 2016 à l'année courante :
client.dl Time.new.year, docs
end
else
puts "# connexion error"
end