Guest User

Untitled

a guest
Jan 10th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.74 KB | None | 0 0
  1. var huf = require('./index');
  2. var j;
  3. const _ = require('underscore');
  4. const cheerio = require('cheerio');
  5. const async = require('async');
  6. var configuration
  7. try {
  8. configuration = require('./config.json');
  9. } catch (e) {
  10. return huf.log(null, 'error', 'Missing, config.json file please create your config file before using hufbot.')
  11. }
  12. var j = require('request').jar();
  13. if (configuration.proxy.active) {
  14. if (configuration.proxy.username == null || configuration.proxy.username == "" && configuration.proxy.password == null || configuration.proxy.password == "") {
  15. // no auth
  16. var proxyUrl = configuration.proxy.host
  17. } else {
  18. // auth
  19. var proxyUrl = "http://" + user + ":" + password + "@" + host + ":" + port;
  20. }
  21. var request = require('request').defaults({
  22. timeout: 30000,
  23. jar: j,
  24. proxy: proxyUrl
  25. });
  26. } else {
  27. var request = require('request').defaults({
  28. timeout: 30000,
  29. jar: j
  30. });
  31. }
  32.  
  33. var stack = []
  34.  
  35. function seek(config, i, callback) {
  36. huf.log(i, 'info', `Task ${i} Started`);
  37. huf.log(i, 'info', 'Seeking for item...')
  38. retry(config)
  39. function retry(config) {
  40. huf.seekForItem(configuration, config.page, config.keywords, (response, err) => {
  41. // Handle Site Crashes
  42. if (err || response == null) {
  43. if (config.autoRetryOnCrash == true) {
  44. return retry(config);
  45. } else {
  46. return huf.log(i, 'error', err);
  47. }
  48. }
  49. return addToCart(i, config, response);
  50. });
  51. }
  52. }
  53.  
  54. for (i in configuration.tasks) {
  55. stack.push(seek(configuration.tasks[i], i))
  56. }
  57.  
  58. async.each(stack, function(res, err) {});
  59.  
  60. function addToCart(i, config, response) {
  61.  
  62. huf.log(i, 'success', `Found your item "${response.name}"`)
  63.  
  64. var link, id;
  65. request({
  66. url: response.link,
  67. method: 'get'
  68. }, function(err, res, body) {
  69.  
  70. if (err) {
  71. return huf.log(i, 'error', 'Error has occured while getting product information.')
  72. } else {
  73. var $ = cheerio.load(body);
  74. }
  75.  
  76. var itemSelectSize = $(`option:contains("${config.size}")`).attr('value');
  77. if (itemSelectSize == undefined) {
  78. return huf.log(i, 'error', `Could not find item available in size ${config.size}`)
  79. } else {
  80. huf.log(i, 'success', `Found item available in size ${config.size}`)
  81. }
  82.  
  83. // if there is only one style open then cop that only one just incase styling was inputted wrong
  84. var variants = [];
  85. $('#product-select option').each(function(i, element) {
  86. var sizeAndPrice = $(this).text().split(" /")[1];
  87. var data = {
  88. id: $(this).attr('value'),
  89. style: $(this).text().split(" /")[0],
  90. size: sizeAndPrice.split(" ")[1]
  91. }
  92. variants.push(data);
  93. });
  94.  
  95. var firstItemStyle = variants[0].style;
  96.  
  97. function isSameStyle(el, index, arr) {
  98. if (index === 0) {
  99. return true;
  100. } else {
  101. return (el.style === arr[index - 1].style);
  102. }
  103. }
  104.  
  105. var isEveryStyleTheSame = variants.every(isSameStyle);
  106.  
  107. if (isEveryStyleTheSame) {
  108. huf.log(i, 'warning', `Every item variant is the same, your item will be checked out in ${variants[0].style}`)
  109. var itemToBuy = _.where(variants, {
  110. style: variants[0].style,
  111. size: config.size
  112. });
  113. huf.log(i, 'info', `Checkout out in ${variants[0].style} in size ${config.size} - ${itemToBuy[0].id}`)
  114. return getSessionID(i, response.link, itemToBuy[0].id, config);
  115. } else {
  116. var itemToBuy = _.where(variants, {
  117. size: config.size
  118. });
  119. if (itemToBuy.indexOf(config.color) > -1) {
  120. return getSessionID(i, response.link, itemToBuy[0].id, config);
  121. } else {
  122. return huf.log(i, 'error', 'Error occured while trying to find your item using the keywords provided.')
  123. }
  124. }
  125.  
  126. });
  127.  
  128. }
  129.  
  130. function getSessionID(i, link, id, config) {
  131. huf.log(i, 'info', 'Collecting Session ID...')
  132. request({
  133. url: 'https://www.rooneyshop.com/cart.js',
  134. method: 'get',
  135. headers: {
  136. 'Accept': 'application/json, text/javascript, */*; q=0.01',
  137. 'X-Requested-With': 'XMLHttpRequest',
  138. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
  139. 'Referer': link,
  140. 'Accept-Encoding': 'gzip, deflate, sdch',
  141. 'Accept-Language': 'en-US,en;q=0.8'
  142. }
  143. }, function(err, res, body) {
  144.  
  145. if (err) {
  146. return huf.log(i, 'error', 'Error has occured while trying to pickup session id')
  147. }
  148.  
  149. return atc(i, link, id, config);
  150.  
  151. });
  152. }
  153.  
  154. function atc(i, link, id, config) {
  155.  
  156. huf.log(i, 'info', 'Adding to Cart...')
  157. huf.log(i, 'info', id)
  158.  
  159. request({
  160. url: 'https://www.rooneyshop.com/cart/add',
  161. followAllRedirects: true,
  162. method: 'post',
  163. headers: {
  164. 'Origin': 'https://www.rooneyshop.com',
  165. 'Upgrade-Insecure-Requests': '1',
  166. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
  167. 'Content-Type': 'application/x-www-form-urlencoded',
  168. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  169. 'Referer': link,
  170. 'Accept-Language': 'en-US,en;q=0.8'
  171. },
  172. formData: {
  173. 'add': 'ADDING.....',
  174. 'id': id
  175. }
  176. }, function(err, res, body) {
  177.  
  178. if (err || body === undefined) {
  179. return huf.log(i, 'error', 'Error occured while trying to add item to your cart.')
  180. } else {
  181. var $ = cheerio.load(body);
  182. }
  183.  
  184. huf.log(i, 'success', '"' + $('.name').text() + '"' + ' added to cart')
  185. huf.log(i, 'success', 'Price: ' + '$' + $('.unit_price').text())
  186.  
  187. return getCheckoutPage(i, id, config);
  188.  
  189. });
  190.  
  191. }
  192.  
  193. function getCheckoutPage(i, id, config) {
  194.  
  195. var idVal = `updates[${id}]`;
  196.  
  197. request({
  198. url: 'https://www.rooneyshop.com/cart',
  199. followAllRedirects: true, // redirects to https checkout
  200. method: 'post',
  201. headers: {
  202. 'Origin': 'https://www.rooneyshop.com',
  203. 'Upgrade-Insecure-Requests': '1',
  204. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36',
  205. 'Content-Type': 'application/x-www-form-urlencoded',
  206. 'Referer': 'http://store.hufworldwide.com/cart',
  207. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  208. 'Accept-Language': 'en-US,en;q=0.8',
  209. 'Cookie': 'cookie'
  210. },
  211. formData: {
  212. 'attributes[nostoref]': '',
  213. 'checkout': 'CHECKOUT',
  214. 'note': '',
  215. idVal: '1'
  216. }
  217. }, function(err, res, body) {
  218.  
  219. if (err || body === undefined) {
  220. return huf.log(i, 'error', 'Error occured while trying to add item to your cart.')
  221. } else {
  222. var $ = cheerio.load(body);
  223. }
  224.  
  225. var cookies = j.getCookies('https://www.rooneyshop.com');
  226. var storeID = $('.edit_checkout').attr('action').split('/')[1];
  227. huf.log(i, 'info', storeID)
  228. var checkoutID = $('.edit_checkout').attr('action').split('checkouts/')[1];
  229. huf.log(i, 'info', 'Checkout ID: ' + checkoutID)
  230. var auth_token = $('input[name=authenticity_token]').attr('value');
  231. huf.log(i, 'info', 'Auth Token Value: ' + auth_token)
  232. return input(i, id, checkoutID, auth_token, config, storeID);
  233.  
  234. });
  235.  
  236. }
  237.  
  238. function input(i, id, checkoutID, auth_token, config, storeID) {
  239.  
  240. if (config.billingInfo.company == null) {
  241. var company = '';
  242. } else {
  243. var company = config.billingInfo.company;
  244. }
  245.  
  246. if (config.billingInfo.aptNumber == null) {
  247. var aptNumber = '';
  248. } else {
  249. var aptNumber = config.billingInfo.aptNumber;
  250. }
  251.  
  252. huf.log(i, 'info', id + ' ' + checkoutID)
  253.  
  254. request({
  255. url: `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  256. followAllRedirects: true, // redirects to https checkout
  257. method: 'get',
  258. headers: {
  259. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  260. 'Accept-Language': 'en-US,en;q=0.8',
  261. 'Host': 'checkout.shopify.com',
  262. 'Referer': `https://checkout.shopify.com/${id}/checkouts/${checkoutID}?step=contact_information`,
  263. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
  264. },
  265. qs: {
  266. 'utf8': '✓',
  267. '_method': 'patch',
  268. 'authenticity_token': auth_token,
  269. 'previous_step': 'contact_information',
  270. 'checkout[email]': config.billingInfo.email,
  271. 'checkout[shipping_address][first_name]': config.billingInfo.firstName,
  272. 'checkout[shipping_address][last_name]': config.billingInfo.lastName,
  273. 'checkout[shipping_address][company]': company,
  274. 'checkout[shipping_address][address1]': config.billingInfo.address,
  275. 'checkout[shipping_address][address2]': aptNumber,
  276. 'checkout[shipping_address][city]': config.billingInfo.city,
  277. 'checkout[shipping_address][country]': config.billingInfo.country,
  278. 'checkout[shipping_address][province]': config.billingInfo.state,
  279. 'checkout[shipping_address][province]': '',
  280. 'checkout[shipping_address][province]': config.billingInfo.stateFull,
  281. 'checkout[shipping_address][zip]': config.billingInfo.zipCode,
  282. 'checkout[shipping_address][phone]': config.billingInfo.phoneNumber,
  283. 'checkout[remember_me]': '',
  284. 'checkout[remember_me]': '0',
  285. 'checkout[client_details][browser_width]': '979',
  286. 'checkout[client_details][browser_height]': '631',
  287. 'checkout[client_details][javascript_enabled]': '1',
  288. 'step': 'contact_information'
  289. }
  290. }, function(err, res, body) {
  291.  
  292. if (err || body === undefined) {
  293. return huf.log(i, 'error', 'Error occured while trying to add item to your cart.')
  294. } else {
  295. var $ = cheerio.load(body);
  296. }
  297.  
  298. huf.log(i, 'info', 'Looking for shipping options, please wait...')
  299. return ship(i, id, checkoutID, storeID, $('input[name=authenticity_token]').attr('value'), config);
  300.  
  301. });
  302.  
  303. }
  304.  
  305. function ship(i, id, checkoutID, storeID, auth_token, config) {
  306.  
  307.  
  308. if (config.billingInfo.company == null) {
  309. var company = '';
  310. } else {
  311. var company = config.billingInfo.company;
  312. }
  313.  
  314. var company = '';
  315.  
  316. if (config.billingInfo.aptNumber == null) {
  317. var aptNumber = '';
  318. } else {
  319. var aptNumber = config.billingInfo.aptNumber;
  320. }
  321.  
  322. request({
  323. url: `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  324. followAllRedirects: true, // redirects to https checkout
  325. method: 'post',
  326. headers: {
  327. 'Origin': 'https://checkout.shopify.com',
  328. 'Content-Type': 'application/x-www-form-urlencoded',
  329. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  330. 'Accept-Language': 'en-US,en;q=0.8',
  331. 'Referer': `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  332. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
  333. },
  334. formData: {
  335. 'utf8': '✓',
  336. '_method': 'patch',
  337. 'authenticity_token': auth_token,
  338. 'button': '',
  339. 'checkout[email]': config.billingInfo.email,
  340. 'checkout[shipping_address][first_name]': config.billingInfo.firstName,
  341. 'checkout[shipping_address][last_name]': config.billingInfo.lastName,
  342. 'checkout[shipping_address][company]': company,
  343. 'checkout[shipping_address][address1]': config.billingInfo.address,
  344. 'checkout[shipping_address][address2]': aptNumber,
  345. 'checkout[shipping_address][city]': config.billingInfo.city,
  346. 'checkout[shipping_address][country]': config.billingInfo.country,
  347. 'checkout[shipping_address][province]': config.billingInfo.stateFull,
  348. 'checkout[shipping_address][zip]': config.billingInfo.zipCode,
  349. 'checkout[shipping_address][phone]': config.billingInfo.phoneNumberFormatted,
  350. 'checkout[remember_me]': '0',
  351. 'checkout[client_details][browser_width]': '979',
  352. 'checkout[client_details][browser_height]': '631',
  353. 'checkout[client_details][javascript_enabled]': '1',
  354. 'previous_step': 'contact_information',
  355. 'step': 'shipping_method'
  356. }
  357. }, function(err, res, body) {
  358. if (err || body === undefined) {
  359. return huf.log(i, 'error', 'Error occured while trying to setup shipping options.')
  360. } else {
  361. var $ = cheerio.load(body);
  362. // if cheerio cant find the first shipping choice retry again
  363. var option_1_radio = $('.input-radio').attr('value');
  364. if ($('.input-radio').eq(1).attr('value') === undefined) {
  365. return ship(i, id, checkoutID, storeID, auth_token, config);
  366. } else {
  367. // shipping options are static after numerous requests
  368. var option_1_radio_dec_val = $('.input-radio').eq(0).attr('value');
  369. var auth = $('input[name=authenticity_token]').attr('value');
  370. huf.log(i, 'success', 'Cheapest Shipping Option Value: ' + option_1_radio_dec_val)
  371. return submitShippingChoice(i, storeID, checkoutID, $('input[name=authenticity_token]').attr('value'), option_1_radio_dec_val, config);
  372. }
  373. }
  374. });
  375. }
  376.  
  377. function submitShippingChoice(i, storeID, checkoutID, auth_token, shippingRateID, config) {
  378. // get to card page
  379. request({
  380. url: `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  381. followAllRedirects: true,
  382. method: 'post',
  383. headers: {
  384. 'Origin': 'https://checkout.shopify.com',
  385. 'Content-Type': 'application/x-www-form-urlencoded',
  386. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  387. 'Accept-Language': 'en-US,en;q=0.8',
  388. 'Referer': `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  389. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
  390. },
  391. formData: {
  392. 'utf8': '✓',
  393. '_method': 'patch',
  394. 'authenticity_token': auth_token,
  395. 'button': '',
  396. 'previous_step': 'shipping_method',
  397. 'step': 'payment_method',
  398. 'checkout[shipping_rate][id]': shippingRateID
  399. }
  400. }, function(err, res, body) {
  401.  
  402. if (err || body === undefined) {
  403. return huf.log(i, 'error', 'Error occured while trying to pay for your item.')
  404. } else {
  405. var $ = cheerio.load(body);
  406. }
  407. huf.log(i, 'success', 'Shipping option has been picked.')
  408. var auth_token = $('input[name=authenticity_token]').attr('value')
  409. var payment_gateway = $('input[name="checkout[payment_gateway]"]').eq(0).attr('value')
  410. var checkout_total_price = $('checkout_total_price').attr('value')
  411. return sendCCInfo(i, auth_token, storeID, checkoutID, config, payment_gateway, checkout_total_price)
  412.  
  413. });
  414.  
  415. }
  416.  
  417. function sendCCInfo(i, auth_token, storeID, checkoutID, config, payment_gateway, checkout_total_price) {
  418. huf.log(i, 'success', 'Sending your CC information to Shopify\'s services...')
  419. request({
  420. url: 'https://elb.deposit.shopifycs.com/sessions',
  421. followAllRedirects: true,
  422. method: 'post',
  423. headers: {
  424. 'accept': 'application/json',
  425. 'Origin': 'https://checkout.shopifycs.com',
  426. 'Accept-Language': 'en-US,en;q=0.8',
  427. 'Host': 'elb.deposit.shopifycs.com',
  428. 'content-type': 'application/json',
  429. 'Referer': `https://checkout.shopifycs.com/number?identifier=${checkoutID}&location=https%3A%2F%2Fcheckout.shopify.com%2F${storeID}%2Fcheckouts%2F${checkoutID}%3Fprevious_step%3Dshipping_method%26step%3Dpayment_method`,
  430. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
  431. },
  432. form: {
  433. 'credit_card': {
  434. 'number': config.cardInfo.number,
  435. 'name': config.cardInfo.nameOnCard,
  436. 'month': config.cardInfo.month,
  437. 'year': config.cardInfo.year,
  438. 'verification_value': config.cardInfo.cvv,
  439. }
  440. }
  441. }, function(err, res, body) {
  442.  
  443. if (err || body === undefined) {
  444. return huf.log(i, 'error', 'Error occured while trying to verify your card with Shopfiy.')
  445. } else {
  446. huf.log(i, 'success', 'Card Information verified by Shopify!')
  447. var cc_verify_id = JSON.parse(body).id
  448. return checkoutCardFinal(i, auth_token, config, storeID, checkoutID, cc_verify_id, payment_gateway, checkout_total_price)
  449. }
  450. });
  451. }
  452.  
  453. function checkoutCardFinal(i, auth_token, config, storeID, checkoutID, cc_verify_id, payment_gateway, checkout_total_price) {
  454. request({
  455. url: `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  456. followAllRedirects: true,
  457. method: 'post',
  458. headers: {
  459. 'Origin': 'https://checkout.shopify.com',
  460. 'Content-Type': 'application/x-www-form-urlencoded',
  461. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  462. 'Accept-Language': 'en-US,en;q=0.8',
  463. 'Referer': `https://checkout.shopify.com/${storeID}/checkouts/${checkoutID}`,
  464. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
  465. },
  466. formData: {
  467. '_method': 'patch',
  468. 'authenticity_token': auth_token,
  469. 'checkout[buyer_accepts_marketing]': '1', // newsletter
  470. 'checkout[client_details][browser_height]': '979',
  471. 'checkout[client_details][browser_width]': '631',
  472. 'checkout[client_details][javascript_enabled]': '1',
  473. 'checkout[credit_card][vault]': 'false',
  474. 'checkout[different_billing_address]': 'false',
  475. 'checkout[payment_gateway]': payment_gateway,
  476. 'checkout[total_price]': checkout_total_price,
  477. 'complete': '1',
  478. 'previous_step': 'payment_method',
  479. 's': cc_verify_id,
  480. 'step': '',
  481. 'utf8': '✓'
  482. }
  483. }, function(err, res, body) {
  484.  
  485. if (err || body === undefined) {
  486. return huf.log(i, 'error', 'Error occured while trying to verify your card with Shopfiy.')
  487. } else {
  488. huf.log(i, 'success', 'Payment Successful!')
  489. }
  490.  
  491. });
  492. }
Add Comment
Please, Sign In to add comment