Guest User

Untitled

a guest
Aug 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import mysql.connector
  2. import json
  3.  
  4. cnx = mysql.connector.connect(
  5. database='idealfit', user='root', password='root', host='localhost'
  6. )
  7.  
  8. cnx1 = mysql.connector.connect(
  9. database='idealfit', user='root', password='root', host='localhost'
  10. )
  11.  
  12. cursor = cnx.cursor();
  13. cursor1 = cnx1.cursor();
  14.  
  15. query = "SELECT a.recipe_id, a.recipe_item_type, b.meta_key, b.meta_value, b.recipe_item_id FROM wp_simmer_recipe_items a, wp_simmer_recipe_itemmeta b WHERE a.recipe_item_id = b.recipe_item_id GROUP BY a.recipe_item_id"
  16. query1 = "SELECT * FROM wp_simmer_recipe_itemmeta WHERE recipe_item_id=(%s)"
  17.  
  18. cursor.execute(query)
  19. rs = cursor.fetchall()
  20.  
  21. data = {}
  22.  
  23. for row in rs:
  24. if row[1] == 'instruction':
  25. cursor1.execute(query1, (row[4],))
  26. insD = cursor1.fetchall()
  27. for instruction in insD:
  28. if instruction[2] != 'is_heading':
  29. data.setdefault(row[0], {'instructions':[], 'ingredients': []})['instructions'].append(instruction[3]);
  30. else:
  31. cursor1.execute(query1, (row[4],))
  32. rd = cursor1.fetchall()
  33. ingredient = {}
  34. for itemMeta in rd:
  35. ingredient[itemMeta[2]] = itemMeta[3]
  36. data.setdefault(row[0], {'ingredients': [], 'instructions': []})['ingredients'].append(ingredient)
  37.  
  38.  
  39. with open('data.json', 'w') as outfile:
  40. json.dump(data, outfile, sort_keys=True, indent=4)
  41.  
  42. cursor1.close()
  43. cnx1.close()
  44. cursor.close()
  45. cnx.close()
Add Comment
Please, Sign In to add comment