aşağıda verdigim kodu xml_url yerine kendi xml urlniiz yapiştirin ve py dosyası olarak kaydedin çalıştırın
url parametreleri şöyle;
siteniz.com/api/products/1 - burda ürün idsini xml den apiye dönüştürüp çekiyor
siteniz.com/api/products?showid=true - burda xmlde olan tüm ürün idlerini json olarak götseriyor..
from flask import Flask, jsonify, request
import requests
import xml.etree.ElementTree as ET
import time
import html
app = Flask(__name__)
# XML dosyasının URL'si
xml_url = 'https://site.com/1.xml'
# Tüm ürünleri saklayacak bir liste
products = []
def process_xml():
global products
# XML dosyasını indirme
response = requests.get(xml_url)
# XML dosyasını bir dosyaya yazma
with open('products.xml', 'wb') as f:
f.write(response.content)
# XML dosyasını açma ve ürünleri alarak listeye ekleme
tree = ET.parse('products.xml')
root = tree.getroot()
products.clear()
for product_element in root.findall('Product'):
product = {
'Id': product_element.get('Id'),
'ModelCode': product_element.get('ModelCode'),
'Sku': product_element.get('Sku'),
'Gtin': product_element.get('Gtin'),
'Name': html.unescape(product_element.get('Name')),
'Manufacturer': html.unescape(product_element.get('Manufacturer')),
'FullDescription': html.unescape(product_element.get('FullDescription')),
'StockQuantity': product_element.get('StockQuantity'),
'Price': product_element.get('Price'),
'OldPrice': product_element.get('OldPrice'),
'Tax': product_element.get('Tax'),
'Pictures': [picture.get('Path') for picture in product_element.findall('./Pictures/Picture')],
'Categories': [html.unescape(category.get('Path')) for category in product_element.findall('./Categories/Category')],
'Combinations': [{
'Id': combination.get('Id'),
'Sku': combination.get('Sku'),
'Gtin': combination.get('Gtin'),
'StockQuantity': combination.get('StockQuantity'),
'OverriddenPrice': combination.get('OverriddenPrice'),
'Attributes': [{'Name': attr.get('Name'), 'Value': attr.get('Value')} for attr in combination.findall('./Attributes/Attribute')]
} for combination in product_element.findall('./Combinations/Combination')],
'Specifications': [{'Name': spec.get('Name'), 'Value': html.unescape(spec.get('Value'))} for spec in product_element.findall('./Specifications/Specification')]
}
products.append(product)
# Flask API endpoint'i
@app.route('/api/products', methods=['GET'])
def get_products():
process_xml()
show_id = request.args.get('showid') # showid parametresini al
if show_id: # Eğer showid parametresi varsa
ids = [product['Id'] for product in products] # Tüm ID'leri al
return jsonify(ids) # Tüm ID'leri JSON formatında gönder
else:
return jsonify(products) # Tüm ürünleri JSON formatında gönder
@app.route('/api/products/<int:id>', methods=['GET'])
def get_product_by_id(id):
process_xml()
product = next((p for p in products if p['Id'] == str(id)), None) # ID'ye göre ürünü bul
if product:
return jsonify(product) # Bulunan ürünü JSON formatında gönder
else:
return jsonify({'message': 'Product not found'}), 404 # Ürün bulunamazsa hata kodu 404 döndür
if __name__ == '__main__':
# İşlemi belirli aralıklarla gerçekleştirme
while True:
process_xml()
time.sleep(60) # 60 saniyede bir kontrol etme
app.run(debug=True)