Created
October 22, 2019 21:15
-
-
Save stevavoliajvar/3e5ef44340f82e26a237f9e2adb71542 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usb/bin/python3 | |
import openpyxl | |
wb = openpyxl.load_workbook('ammo_full.xlsx') | |
TEMPLATE="""--- | |
title: "NAME" | |
price: "TBA" | |
desc: "Opis nije dostupan" | |
img_path: "IMG_PATH" | |
brand: AMMO | |
available: true | |
cat: "CAT" | |
subcat: "SUB" | |
subsubcat: "SS" | |
---""" | |
IM_BP= "/assets/img/" | |
CATS = {"acrylics": "Akrilne boje", "weathering": "Weathering"} | |
subcat = "temp" | |
def mk_f_name(p_name): | |
return p_name.strip().replace(" ", "_").replace("/", "-").replace("\"", "") + ".md" | |
def parse_sheet(sh): | |
print(sh.title) | |
cat = sh.title | |
for row in sh.iter_rows(): | |
if row[1].value == None: | |
print("Current cat is {}".format(row[0].value)) | |
if row[0].value != None: | |
subcat = row[0].value | |
else: | |
p_name = row[1].value.replace("\"", "") | |
f_name = mk_f_name(p_name) | |
img_path = row[0].value + ".jpg" | |
out = TEMPLATE.replace("NAME", p_name)\ | |
.replace("CAT", cat)\ | |
.replace("SUB", subcat) \ | |
.replace("IMG_PATH", IM_BP+img_path) | |
with open("_ammo/" + f_name, "w") as f: | |
f.write(out) | |
print("[+] saved {}".format(f_name)) | |
for sheet in wb: | |
parse_sheet(sheet) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xml.dom import minidom | |
import subprocess | |
import shutil | |
import requests as r | |
import os | |
import time | |
dom = minidom.parse("eduard.xml") | |
BASE_IMG_PATH = "/assets/img" | |
BASE_PRICE = "TBA" | |
TEMPLATE = """--- | |
title: NAME | |
price: TBA | |
desc: Opis nije dostupan | |
img_path: IMG_PATH | |
brand: eduardo | |
available: true | |
cat: | |
subcat: | |
subsubcat: | |
---""" | |
def mk_img_name(p_name, ext): | |
return p_name.replace(" ", "_").replace("/", "-") + "." +ext | |
n = len(dom.getElementsByTagName('SHOPITEM')) | |
for (i,node) in enumerate(dom.getElementsByTagName('SHOPITEM')): | |
shop_item = minidom.parseString(node.toxml()) | |
product_name = shop_item.getElementsByTagName('PRODUCT')[0].firstChild | |
product_img = shop_item.getElementsByTagName('IMGURL')[0].firstChild | |
if hasattr(product_name, 'data'): | |
product_name = product_name.data | |
else: | |
product_name = "None" | |
if hasattr(product_img, 'data'): | |
product_img = product_img.data | |
ext = product_img[-3:] | |
img_path = mk_img_name(product_name, ext) | |
img_data = r.get(product_img, stream=True) | |
if img_data.status_code == 200: | |
with open("img_eduard" + os.sep + img_path, "wb") as f: | |
shutil.copyfileobj(img_data.raw, f) | |
else: | |
product_img = 'None' | |
img_path = "https://picsum.photos/200" | |
with open("_products/" + product_name.replace(" ", "_").replace("/", "-") + ".md", "w") as f: | |
data = TEMPLATE.replace("NAME", product_name).replace("IMG_PATH", img_path) | |
f.write(data) | |
print("[+]NAME: {}, {}/{}".format(product_name, i, n)) | |
print("alle zussamen") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment