2018-04-12 17:06:57 +02:00
|
|
|
#!/usr/bin/env python3.5
|
2014-08-06 11:43:40 +02:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The ZMQ_Feed_Q Module
|
|
|
|
=====================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the ZMQ_Feed_Q Module,
|
|
|
|
And save the paste on disk to allow others modules to work on them.
|
|
|
|
|
|
|
|
..todo:: Be able to choose to delete or not the saved paste after processing.
|
|
|
|
..todo:: Store the empty paste (unprocessed) somewhere in Redis.
|
|
|
|
|
|
|
|
..note:: Module ZMQ_Something_Q and ZMQ_Something are closely bound, always put
|
|
|
|
the same Subscriber name in both of them.
|
|
|
|
|
|
|
|
Requirements
|
|
|
|
------------
|
|
|
|
|
|
|
|
*Need running Redis instances.
|
|
|
|
*Need the ZMQ_Feed_Q Module running to be able to work properly.
|
|
|
|
|
|
|
|
"""
|
2014-08-14 14:11:07 +02:00
|
|
|
import base64
|
|
|
|
import os
|
|
|
|
import time
|
2014-08-06 11:43:40 +02:00
|
|
|
from pubsublogger import publisher
|
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
from Helper import Process
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2018-04-20 10:42:19 +02:00
|
|
|
import magic
|
|
|
|
import io
|
2018-05-02 17:07:10 +02:00
|
|
|
#import gzip
|
2018-04-20 10:42:19 +02:00
|
|
|
|
2018-05-02 17:07:10 +02:00
|
|
|
'''
|
2018-04-20 10:42:19 +02:00
|
|
|
def gunzip_bytes_obj(bytes_obj):
|
|
|
|
in_ = io.BytesIO()
|
|
|
|
in_.write(bytes_obj)
|
|
|
|
in_.seek(0)
|
|
|
|
with gzip.GzipFile(fileobj=in_, mode='rb') as fo:
|
|
|
|
gunzipped_bytes_obj = fo.read()
|
|
|
|
|
2018-05-02 17:07:10 +02:00
|
|
|
return gunzipped_bytes_obj.decode()'''
|
2014-08-14 14:11:07 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
if __name__ == '__main__':
|
2014-08-22 17:35:40 +02:00
|
|
|
publisher.port = 6380
|
2014-08-29 19:37:56 +02:00
|
|
|
publisher.channel = 'Script'
|
2016-07-13 15:57:33 +02:00
|
|
|
processed_paste = 0
|
|
|
|
time_1 = time.time()
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
config_section = 'Global'
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
p = Process(config_section)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
# LOGGING #
|
|
|
|
publisher.info("Feed Script started to receive & publish.")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
message = p.get_from_set()
|
2018-04-12 17:06:57 +02:00
|
|
|
#print(message)
|
2014-08-14 14:11:07 +02:00
|
|
|
# Recovering the streamed message informations.
|
|
|
|
if message is not None:
|
2014-08-29 19:37:56 +02:00
|
|
|
splitted = message.split()
|
|
|
|
if len(splitted) == 2:
|
|
|
|
paste, gzip64encoded = splitted
|
2014-08-06 11:43:40 +02:00
|
|
|
else:
|
2014-08-14 14:11:07 +02:00
|
|
|
# TODO Store the name of the empty paste inside a Redis-list.
|
2018-04-12 17:06:57 +02:00
|
|
|
print("Empty Paste: not processed")
|
2016-07-25 16:38:57 +02:00
|
|
|
publisher.debug("Empty Paste: {0} not processed".format(message))
|
2014-08-06 11:43:40 +02:00
|
|
|
continue
|
|
|
|
else:
|
2018-04-12 17:06:57 +02:00
|
|
|
print("Empty Queues: Waiting...")
|
2016-07-13 15:57:33 +02:00
|
|
|
if int(time.time() - time_1) > 30:
|
|
|
|
to_print = 'Global; ; ; ;glob Processed {0} paste(s)'.format(processed_paste)
|
2018-04-12 17:06:57 +02:00
|
|
|
print(to_print)
|
2016-12-23 15:44:46 +01:00
|
|
|
#publisher.info(to_print)
|
2016-07-13 15:57:33 +02:00
|
|
|
time_1 = time.time()
|
|
|
|
processed_paste = 0
|
2014-08-29 19:37:56 +02:00
|
|
|
time.sleep(1)
|
2014-08-06 11:43:40 +02:00
|
|
|
continue
|
2014-08-14 14:11:07 +02:00
|
|
|
# Creating the full filepath
|
2014-08-22 17:35:40 +02:00
|
|
|
filename = os.path.join(os.environ['AIL_HOME'],
|
2018-04-16 14:50:04 +02:00
|
|
|
p.config.get("Directories", "pastes"), paste)
|
2018-05-02 17:07:10 +02:00
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
dirname = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2018-04-20 10:42:19 +02:00
|
|
|
decoded = base64.standard_b64decode(gzip64encoded)
|
|
|
|
|
2014-08-19 19:07:07 +02:00
|
|
|
with open(filename, 'wb') as f:
|
2018-04-20 10:42:19 +02:00
|
|
|
f.write(decoded)
|
2018-05-02 17:07:10 +02:00
|
|
|
'''try:
|
2018-04-20 10:42:19 +02:00
|
|
|
decoded2 = gunzip_bytes_obj(decoded)
|
|
|
|
except:
|
|
|
|
decoded2 =''
|
|
|
|
|
|
|
|
type = magic.from_buffer(decoded2, mime=True)
|
|
|
|
|
|
|
|
if type!= 'text/x-c++' and type!= 'text/html' and type!= 'text/x-c' and type!= 'text/x-python' and type!= 'text/x-php' and type!= 'application/xml' and type!= 'text/x-shellscript' and type!= 'text/plain' and type!= 'text/x-diff' and type!= 'text/x-ruby':
|
|
|
|
|
|
|
|
print('-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------')
|
|
|
|
print(filename)
|
|
|
|
print(type)
|
|
|
|
print('-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------')
|
2018-05-02 17:07:10 +02:00
|
|
|
'''
|
2018-04-16 14:50:04 +02:00
|
|
|
p.populate_set_out(filename)
|
2016-07-13 15:57:33 +02:00
|
|
|
processed_paste+=1
|