-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan_folder.py
56 lines (46 loc) · 2.63 KB
/
scan_folder.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
import os
from os.path import join
from argparse import ArgumentParser
import pika
from pika import PlainCredentials
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument("-f", "--folder", dest="folder",
help="folder to add in scan queue", metavar="FOLDER")
parser.add_argument("-i", "--file", dest="file",
help="file to add in scan queue", metavar="FILE")
parser.add_argument("-host", "--host", dest="host",
help="RabbitMQ host, default is localhost", metavar="HOST", default='localhost')
parser.add_argument("-n", "--named", dest="named",
help="Indicate that images will be used to name clusters", metavar="NAMED", default=False)
parser.add_argument("-u", "--user", dest="user",
help="RabbitMQ user name", metavar="USER", default='guest')
parser.add_argument("-p", "--password", dest="password",
help="RabbitMQ user password", metavar="PASSWORD", default='guest')
args = parser.parse_args()
if args.folder or args.file:
credentials = PlainCredentials(args.user, args.password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host=args.host, credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='img_queue', durable=True)
if args.folder:
for dirpath, dirs, files in os.walk(args.folder):
for f in files:
if f.lower().endswith(('.jpg', '.jpeg')):
channel.basic_publish(exchange='',
routing_key='img_queue',
body=join(dirpath, f),
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
headers={'named': args.named}
))
if args.file:
if args.file.lower().endswith(('.jpg', '.jpeg')):
channel.basic_publish(exchange='',
routing_key='img_queue',
body=args.file,
properties=pika.BasicProperties(
delivery_mode=2, # make message persistent
headers={'named': args.named}
))
connection.close()