This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagentoclient.py
executable file
·64 lines (57 loc) · 2.22 KB
/
magentoclient.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
http://www.magentocommerce.com/api/soap/introduction.html
http://www.magentocommerce.com/api/soap/catalog/catalogProduct/catalog_product.setSpecialPrice.html
"""
import xmlrpclib
from settings import XMLRPC_URL
import os
import sys
import json
if __name__ == '__main__':
try:
api_user = os.environ['MAGENTO_API_USER']
api_key = os.environ['MAGENTO_API_KEY']
except KeyError:
print "environment variables MAGENTO_API_USER and MAGENTO_API_KEY need to be set."
sys.exit(-1)
#-----------------------------------------------
# start session
#-----------------------------------------------
mclient = xmlrpclib.ServerProxy(XMLRPC_URL)
sessionid = mclient.login(api_user, api_key)
#-----------------------------------------------
# displaying all available resources
#-----------------------------------------------
for r in mclient.resources(sessionid):
for m in r['methods']:
print "{:<50} {}".format(m['path'], m['title'])
print "-" * 120
#-----------------------------------------------
# displaying list of all products
#-----------------------------------------------
prodlist = mclient.call(sessionid, "catalog_product.list")
for p in prodlist[:10]:
print p.keys()
print u"{:<40} {}".format(p["sku"], p["name"])
#-----------------------------------------------
# displaying product info
#-----------------------------------------------
for p in prodlist[:10]:
prodinfo = mclient.call(sessionid, "catalog_product.info", (p["product_id"], "", "", "id"))
#print prodinfo.keys()
pcost = prodinfo.get("cost", 0)
if pcost:
pcost = float(pcost)
pprice = prodinfo.get("price", 0)
if pprice:
pprice = float(pprice)
margin = 0
if pprice and pcost:
margin = (pprice - pcost) / pprice * 100
print u"{:<10} {:<80} {:.>9.2f} {:.>9.2f} {:.>9.2f}".format(p["product_id"], p["name"], pcost or 0, pprice or 0, margin)
#-----------------------------------------------
# end session
#-----------------------------------------------
mclient.endSession(sessionid)