-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy-barcode-details.py
67 lines (54 loc) · 1.62 KB
/
copy-barcode-details.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
#!/bin/python3
import os
import csv
from pymongo import MongoClient
import common
MONGO_URL = os.environ['MONGO_URL']
client = MongoClient(
host="127.0.0.1",
port=27017
)
db = client.off
# collections = db.list_collection_names()
print("Counting documents in 'products' collection...")
length = db['products'].count_documents({})
print ("collections items:", length, "\n")
print('Getting products from the database...')
products = db['products'].find({}, {
'code': 1,
'product_name': 1,
'product_name_en': 1,
'categories_tags': 1,
'lang': 1,
'brands': 1,
'brand_owner': 1,
}).sort('code', -1)
print('Writing to CSV file...')
CsvWriter = csv.writer(open('barcode-database-reverse.csv', 'w'))
print('Writing headers...')
CsvWriter.writerow([
'code',
'product_name',
'product_name_en',
'categories_tags',
'lang',
'brands',
'brand_owner',
])
count = 0
for document in products:
count += 1
code = document.get('code', document.get('id', ''))
CsvWriter.writerow([
# document.get('_id', ''),
code,
document.get('product_name', ''),
document.get('product_name_en', ''),
','.join(document.get('categories_tags', [])),
document.get('lang', 'en'),
document.get('brands', ''),
document.get('brand_owner', ''),
])
# print("{}. writing {}".format(count, code))
# common.printProgressBar(count, prefix = 'Progress:', suffix = 'Complete', decimals=4, length = 100, total=length)
common.printProgressBar(count, prefix = f'{count:,}/{length:,}', suffix = 'Complete', decimals=3, length = 100, total=length)