-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil_funct.py
More file actions
381 lines (315 loc) · 11.8 KB
/
util_funct.py
File metadata and controls
381 lines (315 loc) · 11.8 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 17 16:40:25 2015
On the arduino install the packages :
opkg update #updates the available packages list
opkg install distribute #it contains the easy_install command line tool
opkg install python-openssl #adds ssl support to python
easy_install pip #installs pip
install the following python package on the arduino
pip install httplib2
pip install google-api-python-client
pip install gspread
pip install plotly
first of all :
create a credential file with the get_credential.py
This project need the files :
credential_google.json (file generated by 'get_credential.py')
This project generate the files below
error.log (tracking errors)
event.log (tracking event)...
*******
for raspberry pi it is needed to add path to this directory
if you want to start script from rc.local (at boot)
/home/pi/.local/lib/python2.7/site-packages'
*******
No space left on device: '/usr/lib/python2.7/site-packages/pytz-2015.2.dist-info'
"""
import sys
sys.path.append('/home/pi/.local/lib/python2.7/site-packages') # to start script from rc.local (at boot)
import time # lib pour gestion heure
import httplib2 # lib requette http
import base64
import gspread # lib for google spreadsheet
import json # lib pour fichiers json
import os.path # lib pour test fichiers
import requests
import string
import random
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
from oauth2client.file import Storage
from googleapiclient import errors
from email.mime.text import MIMEText
from twitter import *
'''
DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive '
GMAIL_SCOPE = 'https://mail.google.com/'
SHEET_SCOPE = 'https://spreadsheets.google.com/feeds'
'''
def oauth2_build(scope):
""" query oauth2 to google
:itype : string (scope name)
:rtype : none
"""
currentpathdir = os.path.dirname(os.path.realpath(__file__))
jsonfilename = os.path.join(currentpathdir, "credential_google.json")
storage = Storage(jsonfilename)
credentials = storage.get()
http_auth = httplib2.Http()
credentials.refresh(http_auth)
storage.put(credentials)
http_auth = credentials.authorize(http_auth)
if scope == 'https://www.googleapis.com/auth/drive':
# print 'Drive scope Oauth'
authorisation = build(serviceName='drive', version='v2', http=http_auth)
return authorisation
if scope == 'https://mail.google.com/':
# print 'Gmail scope Oauth'
authorisation = build(serviceName='gmail', version='v1', http=http_auth)
return authorisation
if scope == 'https://spreadsheets.google.com/feeds':
# print 'Sheet scope Oauth'
try:
authorisation = gspread.authorize(credentials)
# print 'Authorisation for SpreadSheets OK'
return authorisation
except gspread.exceptions.GSpreadException:
log_error("Oauth Spreadsheet error")
# print 'Authorisation failure'
def drive_insert_file(file_name, folder_id):
""" envoie un fichier à google drive
:itype : string (the file name)
:itype : string (id of folder)
:rtype : none
"""
if os.path.isfile(file_name):
drive_service = oauth2_build('https://www.googleapis.com/auth/drive')
media_body = MediaFileUpload(file_name, mimetype='*/*', resumable=True)
body = {
'title': file_name,
'mimeType': '*/*',
'parents': [{"kind": "drive#filelink", "id": folder_id}]
}
try:
filehandler = drive_service.files().insert(body=body, media_body=media_body).execute()
file_id = filehandler['id']
textmessage = 'File : %s uploaded . ' % file_name + 'ID File is : %s' % file_id
log_event(textmessage)
# print textmessage
return [file_id]
except errors as e:
error = json.loads(e.content)
error = error['error']['message']
log_error("HttpError in function drive_insert_file() : " + error)
# print 'An error occured: %s' % error
else:
log_error("file %s doesn't exist in function : drive_insert_file() : " % file_name)
exit()
def drive_delete_file(file_id):
""" delete a file in drive
:itype : string (id of the file)
:rtype : none
"""
drive_service = oauth2_build('https://www.googleapis.com/auth/drive')
try:
drive_service.files().delete(fileId=file_id).execute()
textmessage = 'File : %s deleted' % file_id
log_event(textmessage)
except errors as e:
error = json.loads(e.content)
error = error['error']['message']
log_error("HttpError in function : drive_delete_file() : " + error)
exit()
def print_files_in_folder(folder_id):
""" affiche les id des fichiers du répertoire
:itype : string of the folder id
:rtype : none
"""
drive_service = oauth2_build('https://www.googleapis.com/auth/drive')
page_token = None
while True:
try:
param = {}
if page_token:
param['pageToken'] = page_token
children = drive_service.children().list(folderId=folder_id, **param).execute()
if not len(children['items']):
log_error(
"No File in folder %s \r\t\t\t\t\tor bad folder_id in function : print_files_in_folder() " % folder_id)
break
for child in children.get('items', []):
print(child['id'])
# file = service.files().get(fileId=child['id']).execute()
# print 'Title: %s' % file['title']
page_token = children.get('nextPageToken')
if not page_token:
break
except errors as e:
error = json.loads(e.content)
error = error['error']['message']
log_error("HttpError in function : print_files_in_folder() : " + error)
break
exit()
def gmaillistmessage():
""" liste les messages de la boite mail
:itype : none
:rtype : print the ID
"""
gmail_service = oauth2_build('https://mail.google.com/')
try:
threads = gmail_service.users().threads().list(userId='me').execute()
if threads['threads']:
for thread in threads['threads']:
print('Thread ID: %s' % (thread['id']))
except errors as e:
error = json.loads(e.content)
error = error['error']['message']
log_error("HttpError in function : gmaillistmessage() : " + error)
exit()
def gmailsendmessage(message):
""" envoie le message par email
:itype : string text message
:rtype : none
"""
text_message = gmailcreatemessage(message)
gmail_service = oauth2_build('https://mail.google.com/')
try:
message = (gmail_service.users().messages().send(userId='me', body=text_message)
.execute())
# print 'Message Id: %s' % message['id']
log_event("Email Message Id: %s sent" % message['id'])
except errors as e:
error = json.loads(e.content)
error = error['error']['message']
log_error("HttpError in function : gmailsendmessage()" + error)
exit()
def gmailcreatemessage(message_text):
""" creation du message à envoyer pour email
:itype : string text message
:rtype : string raw encoded
"""
currentpathdir = os.path.dirname(os.path.realpath(__file__))
jsonfilename = os.path.join(currentpathdir, "config.json")
data_email = get_json_data_from_file(jsonfilename)
message = MIMEText(message_text)
message['to'] = data_email['dest_mail']
message['from'] = data_email['source_mail']
message['subject'] = "Yuno info"
return {'raw': base64.b64encode(message.as_string())}
def get_json_data_from_file(file_name):
""" Return the data object from the json file in a python dictionnary.
:itype : file name
:rtype : dictionnary
"""
try:
json_data = open(file_name).read()
except IOError:
log_error("IOError in function function : get_json_data_from_file()")
exit()
else:
python_data = json.loads(json_data)
return python_data
def check_internet():
""" test cnx internet
:itype : None
:rtype : bool
"""
try:
_ = requests.get('http://www.google.fr/', timeout=4)
log_event("Internet is Up !")
return True
except requests.ConnectionError:
log_error("No internet connection available.")
return False
def get_ip_address():
""""
get the current WAn ip address
:itype none
:rtype string
"""
url = "http://checkip.dyndns.org"
request = requests.get(url)
clean = request.text.split(': ', 1)[1]
your_ip = str(clean.split('</body></html>', 1)[0])
return your_ip
def email_ip_addr():
""""
send by email the IP address
:itype none
:rtype string
"""
gmailsendmessage(get_ip_address())
def tweet_message(msg):
""" send a text message on twitter
:itype : string
:rtype : none
"""
char_rdn = random.choice(string.ascii_letters)
currentpathdir = os.path.dirname(os.path.realpath(__file__))
jsonfilename = os.path.join(currentpathdir, "credential.txt")
data_tweet = get_json_data_from_file(jsonfilename)
token = data_tweet['Twitter_API_Access_Token']
token_secret = data_tweet['Twitter_API_Access_Token_Secret']
consumer_key = data_tweet['Twitter_API_Key']
consumer_secret = data_tweet['Twitter_API_Secret_Key']
t = Twitter(auth=OAuth(token, token_secret, consumer_key, consumer_secret))
message = msg + " " + char_rdn
try:
t.statuses.update(status=message)
except (TwitterError, ConnectionError) as err:
log_error("Twitter error..." + err)
def tweet_ip_address():
""" send the ip address on twitter
:itype : none
:rtype : none
"""
char_rdn = random.choice(string.ascii_letters)
currentpathdir = os.path.dirname(os.path.realpath(__file__))
jsonfilename = os.path.join(currentpathdir, "credential.txt")
data_tweet = get_json_data_from_file(jsonfilename)
token = data_tweet['Twitter_API_Access_Token']
token_secret = data_tweet['Twitter_API_Access_Token_Secret']
consumer_key = data_tweet['Twitter_API_Key']
consumer_secret = data_tweet['Twitter_API_Secret_Key']
t = Twitter(auth=OAuth(token, token_secret, consumer_key, consumer_secret))
message = get_ip_address() + " " + char_rdn
try:
t.statuses.update(status=message)
except (TwitterError, ConnectionError) as err:
log_error("Twitter error..." + err)
def log_error(error_message):
""" log to the file error.log the current error with the date
:itype : string message
:rtype : None
"""
currentpathdir = os.path.dirname(os.path.realpath(__file__))
logfile = os.path.join(currentpathdir, "error.log")
now = str(time.strftime("%c"))
f = open(logfile, "a")
f.write(now + " " + error_message + "\r\n")
f.close()
def log_event(event_message):
""" log to the file event.log the current event with the date
:itype : string message
:rtype : None
"""
currentpathdir = os.path.dirname(os.path.realpath(__file__))
logfile = os.path.join(currentpathdir, "event.log")
now = str(time.strftime("%c"))
f = open(logfile, "a")
f.write(now + " " + event_message + "\r\n")
f.close()
# drive_delete_file("0B9Yp8cxBtjfea2xiU3VEblRsaE0")
# File_Id = drive_insert_file("teleinfo.log",test_folder)
# folder = get_json_data_from_file("config.json")
# folder = folder['test_folder']
# print_files_in_folder(folder)
# gmaillistmessage()
# gmailsendmessage("test de message")
# check_internet()
# print str(get_index())
# put_index(12355)
# print str(get_index())
# tweet_message("hel i raapi")