Guest User

Untitled

a guest
Oct 11th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. import requests
  2.  
  3. def send_graphql_query(query, variables=None):
  4. headers = {
  5. 'accept': '*/*',
  6. 'accept-language': 'pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7',
  7. 'content-type': 'application/json',
  8. 'origin': 'https://sydneytools.com.au',
  9. 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36',
  10. }
  11.  
  12. json_data = {
  13. 'query': query,
  14. 'variables': variables or {},
  15. }
  16.  
  17. response = requests.post('https://sydneytools.com.au/graphql', headers=headers, json=json_data)
  18. return response.json()
  19.  
  20. # Updated query for product catalogue
  21. query = '''
  22. query routes_Catalogue_Query(
  23. $categoryUrlSlug: String!
  24. $subcategoryUrlSlug: String
  25. $subcategoryUrlSlug2: String
  26. $brandUrlSlug: String
  27. $count: Int!
  28. $cursor: String
  29. $filters: String
  30. $orderBy: OrderBy
  31. $page: Int
  32. $priorityPage: Boolean = false
  33. ) {
  34. viewer {
  35. ...Catalogue_viewer_3JTFNo
  36. id
  37. }
  38. }
  39.  
  40. fragment Catalogue_viewer_3JTFNo on Customer {
  41. ...List_viewer
  42. ...CmsBanner_viewer
  43. ...PriorityPage_viewer @include(if: $priorityPage)
  44. configs
  45. categories(first: 1, urlSlug: $categoryUrlSlug) {
  46. edges {
  47. node {
  48. faqs(first: 99) {
  49. edges {
  50. node {
  51. id
  52. question
  53. answer
  54. }
  55. }
  56. }
  57. id
  58. name
  59. urlSlug
  60. description
  61. description2
  62. metaTitle
  63. metaDescription
  64. previewDescription
  65. jsonLd
  66. featuredBrands(first: 999) {
  67. edges {
  68. node {
  69. id
  70. name
  71. imageUrl
  72. urlSlug
  73. }
  74. }
  75. }
  76. catalogues(first: $count, orderBy: $orderBy, after: $cursor, filters: $filters, brandUrlSlug: $brandUrlSlug, subcategoryUrlSlug: $subcategoryUrlSlug, subcategoryUrlSlug2: $subcategoryUrlSlug2, page: $page) {
  77. totalCount
  78. subcategory {
  79. urlSlug
  80. name
  81. canonicalUrl
  82. metaTitle
  83. metaDescription
  84. description
  85. description2
  86. faqs(first: 99) {
  87. edges {
  88. node {
  89. id
  90. question
  91. answer
  92. }
  93. }
  94. }
  95. id
  96. }
  97. brand {
  98. urlSlug
  99. metaTitle
  100. description
  101. description2
  102. name
  103. id
  104. }
  105. facets {
  106. __typename
  107. __isFacetType: __typename
  108. ... on Facet {
  109. key
  110. name
  111. position
  112. buckets {
  113. key
  114. docCount
  115. imageUrl
  116. }
  117. }
  118. ... on FacetStats {
  119. key
  120. name
  121. position
  122. buckets {
  123. key
  124. docCount
  125. to
  126. from
  127. }
  128. max
  129. min
  130. }
  131. }
  132. edges {
  133. node {
  134. __typename
  135. ... on Subcategory {
  136. id
  137. imageUrl
  138. imageMode
  139. link
  140. displayMode
  141. name
  142. position
  143. urlSlug
  144. docCount
  145. metaTitle
  146. metaDescription
  147. }
  148. ... on Product {
  149. id
  150. name
  151. adjacentBanners(first: 99) {
  152. edges {
  153. node {
  154. id
  155. name
  156. placement
  157. imageUrl
  158. link
  159. }
  160. }
  161. }
  162. ...ProductCard_product
  163. }
  164. ... on Node {
  165. __isNode: __typename
  166. id
  167. }
  168. }
  169. cursor
  170. }
  171. pageInfo {
  172. endCursor
  173. hasNextPage
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180.  
  181. fragment CmsBanner_viewer on Customer {
  182. cmsBanners(first: 999) {
  183. edges {
  184. node {
  185. id
  186. title
  187. link
  188. urlPaths
  189. categoryBig
  190. categorySmall
  191. }
  192. }
  193. }
  194. }
  195.  
  196. fragment List_viewer on Customer {
  197. id
  198. ...ProductCard_viewer
  199. }
  200.  
  201. fragment PriorityPage_viewer on Customer {
  202. id
  203. email
  204. faqs(first: 99, filterBy: [{field: "name", filter: "[PRIORITY-SHIPPING]", filterType: "text", type: "contains"}]) {
  205. edges {
  206. node {
  207. id
  208. question
  209. answer
  210. }
  211. }
  212. }
  213. }
  214.  
  215. fragment ProductCard_product on Product {
  216. id
  217. alerts
  218. name
  219. type
  220. configurables(first: 99) {
  221. edges {
  222. node {
  223. id
  224. type
  225. discountCode
  226. name
  227. model
  228. secondModel
  229. sku
  230. regularPrice
  231. price
  232. priceMatch
  233. sellable
  234. nonStock
  235. }
  236. }
  237. }
  238. model
  239. secondModel
  240. regularPrice
  241. price
  242. priceDrop
  243. sku
  244. urlSlug
  245. freeShipping
  246. priorityShipping {
  247. getByDate
  248. }
  249. globalFreeShipping
  250. preorderDate
  251. backInStockDate
  252. nonStock
  253. onlineOnly
  254. callForPrice
  255. inStoreOnly
  256. sellable
  257. clearance
  258. discountCode
  259. brand {
  260. id
  261. name
  262. }
  263. loyaltyPoints {
  264. accruing
  265. dollars
  266. }
  267. labels(first: 999) {
  268. edges {
  269. node {
  270. id
  271. imageUrl
  272. placement
  273. type
  274. }
  275. }
  276. }
  277. mainImage {
  278. id
  279. thumbnail
  280. }
  281. }
  282.  
  283. fragment ProductCard_viewer on Customer {
  284. id
  285. freeShippingAmount
  286. }
  287. '''
  288.  
  289. # Example usage with variables
  290. variables = {
  291. "categoryUrlSlug": "power-tools",
  292. "count": 20,
  293. "page": 1,
  294. "priorityPage": False
  295. }
  296.  
  297. result = send_graphql_query(query, variables)
  298.  
  299. # Function to print subcategory information
  300. def print_subcategory_info(subcategory):
  301. print(f"Subcategory: {subcategory['name']}")
  302. print(f"Image URL: {subcategory['imageUrl']}")
  303. print(f"Product Count: {subcategory['docCount']}")
  304. print("-" * 40)
  305.  
  306. # Parse and print the results
  307. if 'data' in result and 'viewer' in result['data']:
  308. viewer = result['data']['viewer']
  309. if 'categories' in viewer and 'edges' in viewer['categories']:
  310. for category_edge in viewer['categories']['edges']:
  311. category = category_edge['node']
  312. if 'catalogues' in category and 'edges' in category['catalogues']:
  313. for subcategory_edge in category['catalogues']['edges']:
  314. subcategory = subcategory_edge['node']
  315. if subcategory['__typename'] == 'Subcategory':
  316. print_subcategory_info(subcategory)
  317. else:
  318. print("No category or subcategory information found.")
  319. else:
  320. print("Unexpected response structure.")
Advertisement
Add Comment
Please, Sign In to add comment