Plutonergy

MKM order organizer

Mar 19th, 2021
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.02 KB | None | 0 0
  1. from database_stuff  import  DB, MTG, mtgcursor, sqliteconnection
  2. from database_stuff  import  sqlitecursor
  3. from mkm             import  mkm_api
  4. from tricks          import  tech
  5. import copy
  6. import sys
  7.  
  8. def gather_all_orders():
  9.    
  10.     def test_build_sqlite_table(cls, table, eachorder, eachkey):
  11.         try:
  12.             getattr(cls, eachkey)
  13.             tmp_key = db_sqlite(table, eachkey)
  14.             return tmp_key
  15.         except AttributeError:
  16.             if eachorder[eachkey] == None or eachorder[eachkey] == "":
  17.                 return False
  18.  
  19.             elif type(eachorder[eachkey]) == float:
  20.                 tmp_key = db_sqlite(table, eachkey, 'FLOAT')
  21.  
  22.             elif type(eachorder[eachkey]) == int or type(eachorder[eachkey]) == bool:
  23.                 tmp_key = db_sqlite(table, eachkey, 'INTEGER')
  24.  
  25.             elif type(eachorder[eachkey]) == str:
  26.                 tmp_key = db_sqlite(table, eachkey, 'TEXT')
  27.  
  28.             else:
  29.                 return False
  30.  
  31.             setattr(cls, eachkey, tmp_key)
  32.             print('Created new column in order_head ->', eachkey, type(eachorder[eachkey]))
  33.  
  34.             return tmp_key
  35.        
  36.     def value_filler(head_values_alpha, eachorder, eachkey, table):
  37.         """
  38.        takes a list with values and add new values into that list before returning it, also: DB.class is updated
  39.        :param head_values_alpha: this list comes in and goes back
  40.        :param eachorder: MKM dictionary
  41.        :param eachkey: specified key for MKM dictionary
  42.        :param table: sqlite table
  43.        :return: head_values_alpha
  44.        """
  45.         typelist = [str, int, float, bool]
  46.         if type(eachorder[eachkey]) not in typelist:
  47.             return head_values_alpha
  48.  
  49.         rv_key = tech.test_build_sqlite_table(DB.order_head, table, eachorder, eachkey)
  50.  
  51.         if rv_key == False:
  52.             return head_values_alpha
  53.  
  54.         else:
  55.             _, head_values = tech.empty_insert_query(sqlitecursor, table)
  56.             head_values[rv_key] = eachorder[eachkey]
  57.  
  58.             for head_count in range(len(head_values_alpha)):
  59.                 if type(head_values_alpha[head_count]) in typelist and head_values_alpha[head_count] != "":
  60.                     head_values[head_count] = head_values_alpha[head_count]
  61.  
  62.             return head_values
  63.  
  64.     def create_values_input(**kwargs):
  65.         """
  66.        takes all keys from order.json at each level and sets those keys via value_filler
  67.        if root present i kwargs tree is scanned for keys, then list of branches and leafs
  68.        :return: appends lists into list and return list-list
  69.        """
  70.         rvlist = []
  71.  
  72.         order = kwargs['order']
  73.         branch = kwargs['branch']
  74.         leaf = kwargs['leaf']
  75.         table = kwargs['table']
  76.  
  77.         c = {'tree': order, 'bransch': branch, 'leaf': leaf}
  78.  
  79.         if 'root' in kwargs:
  80.             _, head_values_alpha = tech.empty_insert_query(sqlitecursor, table)
  81.             allkeys = order.keys()
  82.             for key in allkeys:
  83.                 head_values_alpha = value_filler(head_values_alpha, order, key, table)
  84.             rvlist.append(head_values_alpha)
  85.  
  86.         for level in c['bransch']:
  87.             _, head_values_alpha = tech.empty_insert_query(sqlitecursor, table)
  88.  
  89.             allkeys = c['tree'][level].keys()
  90.             for key in allkeys:
  91.                 head_values_alpha = value_filler(head_values_alpha, c['tree'][level], key, table)
  92.  
  93.             for leaf in c['leaf']:
  94.                 allkeys = c['tree'][level][leaf].keys()
  95.                 for key in allkeys:
  96.                     head_values_alpha = value_filler(head_values_alpha, c['tree'][level][leaf], key, table)
  97.  
  98.             rvlist.append(head_values_alpha)
  99.  
  100.         return rvlist
  101.  
  102.     def extract_user(eachorder):
  103.         """
  104.        takes both seller and buyer from order.json and store
  105.        their user details into a mkm_user database
  106.        :param eachorder: order.json dictionary
  107.        """
  108.         rvlist = create_values_input(order=eachorder, branch=['seller', 'buyer'], leaf=['name', 'address'], table='mkm_user')
  109.  
  110.         for head_values_alpha in rvlist:
  111.  
  112.             query, _ = tech.empty_insert_query(sqlitecursor, 'mkm_user')
  113.  
  114.             sqlitecursor.execute('select * from mkm_user where idUser = (?)', (head_values_alpha[DB.mkm_user.idUser],))
  115.             data = sqlitecursor.fetchone()
  116.  
  117.             if data != None:
  118.                 with sqliteconnection:
  119.                     sqlitecursor.execute('delete from mkm_user where idUser = (?)', (head_values_alpha[DB.mkm_user.idUser],))
  120.  
  121.                 data = list(data)
  122.  
  123.                 for column in range(len(head_values_alpha)):
  124.                     if head_values_alpha[column] != None:
  125.                         data[column] = head_values_alpha[column]
  126.  
  127.                 head_values_alpha = data
  128.  
  129.             with sqliteconnection:
  130.                 sqlitecursor.execute(query, head_values_alpha)
  131.  
  132.     def make_order_head(eachorder):
  133.         """
  134.        makes an order header from order.json
  135.        :param eachorder: order.json dictionary
  136.        """
  137.         try:
  138.             eachorder['shippingMethod']['shippingMethod'] = eachorder['shippingMethod']['name']
  139.             eachorder['shippingMethod'].pop('name')
  140.         except KeyError:
  141.             pass
  142.  
  143.         rvlist = create_values_input(order=eachorder, branch=['state', 'shippingMethod', 'shippingAddress'], leaf=[], table='order_head', root=True)
  144.  
  145.         head_values_alpha = rvlist[-1]
  146.  
  147.         for count in range(0, len(rvlist)-1):
  148.             for count_count in range(len(rvlist[count])):
  149.                 if rvlist[count][count_count] != None:
  150.                     head_values_alpha[count_count] = rvlist[count][count_count]
  151.  
  152.         head_values_alpha[DB.order_head.seller.idUser_seller] = eachorder['seller']['idUser']
  153.         head_values_alpha[DB.order_head.buyer.idUser_buyer] = eachorder['buyer']['idUser']
  154.  
  155.         with sqliteconnection:
  156.             head_query_alpha, _ = tech.empty_insert_query(sqlitecursor, 'order_head')
  157.             sqlitecursor.execute(head_query_alpha, head_values_alpha)
  158.  
  159.     def make_orders(rv):
  160.         query, values = tech.empty_insert_query(sqlitecursor, 'orders')
  161.  
  162.         executemanylist = []
  163.         cycle = {
  164.             DB.orders.idProduct: ['idProduct', int],
  165.             DB.orders.language: ['language', 'languageName', str],
  166.             DB.orders.comments: ['comments', str],
  167.             DB.orders.price: ['price', float],
  168.             DB.orders.condition: ['condition', str],
  169.             DB.orders.foil: ['isFoil', bool],
  170.             DB.orders.amount: ['count', int],
  171.         }
  172.  
  173.         if 'order' not in rv:
  174.             print("Error: found no orders in JSON!")
  175.             return
  176.  
  177.         for eachorder in rv['order']:
  178.  
  179.             if 'article' in eachorder and 'idOrder' in eachorder:
  180.                 sqlitecursor.execute('select * from order_head where idOrder = (?)', (eachorder['idOrder'],))
  181.                 data = sqlitecursor.fetchone()
  182.  
  183.                 if data != None:
  184.                     continue
  185.             else:
  186.                 continue
  187.  
  188.             make_order_head(eachorder)
  189.             extract_user(eachorder)
  190.  
  191.             values[DB.orders.idOrder] = eachorder['idOrder']
  192.  
  193.             for eacharticle in eachorder['article']:
  194.                 _values = copy.copy(values)
  195.  
  196.                 for dbkey, v in cycle.items():
  197.                     vvv = copy.copy(eacharticle)
  198.  
  199.                     for checkcount in range(0, len(v)-1):
  200.                         vv = v[checkcount]
  201.  
  202.                         try: vvv = vvv[vv]
  203.                         except KeyError: pass
  204.  
  205.                     if type(vvv) == v[-1] and vvv != "":
  206.                         _values[dbkey] = vvv
  207.  
  208.                 if 'isPlayset' in eacharticle:
  209.                     if eacharticle['isPlayset'] == True and type(_values[DB.orders.amount]) == int:
  210.                         _values[DB.orders.amount] = _values[DB.orders.amount] * 4
  211.  
  212.                 if type(_values[DB.orders.condition]) == str:
  213.                     for grade in range(7):
  214.                         if DB.grade(grade)[0] == _values[DB.orders.condition]:
  215.                             _values[DB.orders.condition] = grade
  216.  
  217.                 if _values[DB.orders.foil] == True:
  218.                     _values[DB.orders.foil] = 1
  219.                 else:
  220.                     _values[DB.orders.foil] = 0
  221.  
  222.                 if _values[DB.orders.idProduct] != None:
  223.                     mtgcursor.execute('select * from cards where mcmId = (?)', (_values[DB.orders.idProduct],))
  224.                     mtgjsondata = mtgcursor.fetchone()
  225.  
  226.                     if mtgjsondata != None:
  227.                         _values[DB.orders.uuid] = mtgjsondata[MTG.cards.uuid]
  228.  
  229.                 executemanylist.append(tuple(_values))
  230.  
  231.         if len(executemanylist) > 0:
  232.             with sqliteconnection:
  233.                 sqlitecursor.executemany(query, executemanylist)
  234.  
  235.     catchrange = 1
  236.     rv = mkm_api('orders/buyer/received/' + str(catchrange))
  237.  
  238.     if rv != False:
  239.         make_orders(rv)
Advertisement
Add Comment
Please, Sign In to add comment