import csv
from collections import defaultdict
import xmlrpc.client
import requests

ODOO_URL = 'http://localhost:16001'
ODOO_DB = 'hazard_new'
ODOO_USER = 'admin'
ODOO_PASS = 'admin'

def get_shopify_credentials():
    common = xmlrpc.client.ServerProxy(f'{ODOO_URL}/xmlrpc/2/common')
    uid = common.authenticate(ODOO_DB, ODOO_USER, ODOO_PASS, {})
    models = xmlrpc.client.ServerProxy(f'{ODOO_URL}/xmlrpc/2/object')
    config_ids = models.execute_kw(ODOO_DB, uid, ODOO_PASS, 'shopify.config', 'search', [[]])
    config = models.execute_kw(ODOO_DB, uid, ODOO_PASS, 'shopify.config', 'read', [config_ids[0]], {'fields': ['shop_url', 'access_token']})[0]
    return config.get('shop_url'), config.get('access_token')

shop_url, access_token = get_shopify_credentials()
headers = {'X-Shopify-Access-Token': access_token, 'Content-Type': 'application/json'}
query = """
{
  collection(id: "gid://shopify/Collection/487990952129") {
    products(first: 250) {
      edges {
        node { legacyResourceId title }
      }
    }
  }
}
"""
resp = requests.post(f"https://{shop_url}/admin/api/2023-10/graphql.json", headers=headers, json={'query': query})
product_titles = [edge['node']['title'] for edge in resp.json()['data']['collection']['products']['edges']]

print("PRODUCTOS EN LA COLECCIÓN PADEL 78:")
for t in product_titles:
    print(t)
