Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.82 KB | None | 0 0
  1. const fs = require('fs');
  2. const lodashArray = require('lodash/array');
  3. const webdriverio = require('webdriverio');
  4. const options = {debug:true,desiredCapabilities: { browserName: 'chrome', platform: 'XP'} };
  5. const client = webdriverio.remote(options);
  6. const async = require('async');
  7.  
  8. var path = require('path');
  9.  
  10. let loginURL = 'https://www.gumtree.pl/login.html';
  11.  
  12. var add_title="Moje ogłoszenie 123";
  13.  
  14. var request = require("request")
  15.  
  16.  
  17. class Gumtree {
  18. static login(user) {
  19. console.log("login: "+user.username);
  20. console.log("Password:"+user.password);
  21. console.log("URL:" +loginURL);
  22. return new Promise((resolve, reject) => {
  23. client
  24. .init()
  25. .url(loginURL)
  26. .pause(500)
  27. .setValue('#login input[name=email]', user.username)
  28. .pause(500)
  29. .setValue('#login input[name=password]', user.password)
  30. .pause(500)
  31. .click('#login-button')
  32. .pause(500)
  33. .call(() => {
  34. let url = client.getUrl();
  35. let tmpInterval = setInterval(() => {
  36. async.parallel({
  37. url: (callback) => {
  38. client.getUrl().then(url => {
  39. callback(null, url);
  40. })
  41. },
  42. loginButtonExist:(callback) =>{
  43. client.isExisting('#login-button').then(exist => {
  44. callback(null, exist);
  45. });
  46. }
  47. },
  48. (err, results) => {
  49. if (err) {
  50. reject(err);
  51. }
  52. if (results.loginButtonExist === false) { //logowanie sie powiodlo bo zniknal button
  53. console.log('login Successfull');
  54. resolve();
  55. clearInterval(tmpInterval);
  56. }else{
  57. reject('unsuccessful login, bad/password probably');
  58. clearInterval(tmpInterval);
  59. }
  60.  
  61. });
  62. }, 500);
  63. });
  64. })
  65. }
  66.  
  67. static gotoads() {
  68. return new Promise((resolve, reject) => {
  69. client
  70. .url('http://www.gumtree.pl/my/ads.html')
  71. .pause(500)
  72. .call(() => {
  73. resolve();
  74. });
  75. })
  76. }
  77.  
  78.  
  79.  
  80. static add_ad() {
  81. console.log("ADD_AD");
  82. return new Promise((resolve, reject) => {
  83. client
  84. .url('https://www.gumtree.pl/post.html')
  85. .pause(500)
  86. .click('li[data-id="9"]').call(() => {
  87.  
  88. // od tego momentu na rozne sposoby chce kliknac podkategorie, a potem wybor wojewodztwa i miasta, reszte sobie poradze bo to dziala jak jakims
  89. // cudem tam dotarlem tym slabym kodem nizej nizej ;d
  90.  
  91.  
  92. var isVisible = client.isVisible('li[data-id="9105"]').then(function (k) {
  93. console.log(k);
  94. });
  95. console.log(isVisible);
  96.  
  97.  
  98.  
  99. }).then(function(){
  100.  
  101. client.waitUntil(() => {
  102.  
  103. var isVisible = this.isVisible('li[data-id="9105"]');
  104. console.log(isVisible);
  105.  
  106. return isVisible;
  107.  
  108. }, 100000, '')}).pause(10000).then(function(){
  109.  
  110. console.log("is visible");
  111. this.click('li[data-id="9105"]');
  112.  
  113. var subcategory_click = false;
  114. client.waitForVisible('li[data-id="9105"]');
  115.  
  116. let mainInterval = setInterval(() => {
  117.  
  118. this.pause(500);
  119.  
  120. this.isExisting('li[data-id="9105"]').then(isVisible => {
  121. console.log("is existing"+isVisible);
  122.  
  123. // this.click('li[data-id="9105"]');
  124. });
  125.  
  126.  
  127. client.waitForVisible('li[data-id="9105"]');
  128.  
  129.  
  130.  
  131. var isVisible = this.isVisible('li[data-id="9105"]');
  132.  
  133. console.log(isVisible);
  134. // console.log(subcategory_click);
  135. this.click('li[data-id="9105"]').pause(500);
  136. if (isVisible === true && !subcategory_click) {
  137. console.log('li[data-id="9105"] visible')
  138. this.click('li[data-id="9105"]').pause(500);
  139. subcategory_click = true;
  140. }
  141.  
  142.  
  143.  
  144. },1000);
  145.  
  146.  
  147. })
  148. .pause(1500)
  149.  
  150.  
  151. })
  152. }
  153.  
  154.  
  155.  
  156.  
  157. static add_ad_old() {
  158. console.log("ADD_AD");
  159.  
  160.  
  161.  
  162.  
  163. return new Promise((resolve, reject) => {
  164. client
  165. .url('https://www.gumtree.pl/post.html')
  166. .pause(500)
  167. .click('li[data-id="9"]').then(function(){
  168.  
  169. var subcategory_click = false;
  170.  
  171. client.waitForVisible('li[data-id="9105"]')
  172. .then(function(visible){
  173. console.log("waitforvisible");
  174. console.log("visible: "+visible);
  175.  
  176. });
  177.  
  178. let mainInterval = setInterval(() => {
  179.  
  180.  
  181.  
  182. this.isExisting('li[data-id="9105"]').then(isVisible => {
  183. if (isVisible === true && !subcategory_click) {
  184. console.log('li[data-id="9105"] visible')
  185. this.click('li[data-id="9105"]').pause(500);
  186. subcategory_click = true;
  187. }
  188. });
  189.  
  190. },10000);
  191.  
  192.  
  193. })
  194. .pause(1500)
  195. .waitForVisible('li[data-id="9105"]')
  196. .then(function(visible){
  197. console.log(visible);
  198. console.log("is isible now");
  199.  
  200. //this.click('li[data-id="9105"]').pause(500);
  201. this.url('https://www.gumtree.pl/post.html')
  202. this.click('li[data-id="9105"]').pause(500);
  203.  
  204.  
  205. this.pause(1500);
  206.  
  207. // if (this.alertText()) {
  208. // this.alertAccept();
  209. // }
  210. //this.click('li[data-id="9105"]').pause(500);
  211.  
  212. console.log("przed visible");
  213.  
  214. /*
  215. var elem = this.element("#locSelector").then(function(cos) {
  216. console.log("elem:");
  217. console.log(elem);
  218. console.log(cos);
  219. console.log(this);
  220.  
  221. });
  222. console.log("elem:");
  223. console.log(elem);
  224. */
  225.  
  226. var firstCategoryClicked = false;
  227.  
  228. this.alertAccept().then(function() {
  229. console.log("alert accept");
  230. client.pause(1000);
  231.  
  232. let tmpInterval = setInterval(() => {
  233. console.log("Interval");
  234.  
  235. if(client.isVisible('li[data-id="3200002"]') && !firstCategoryClicked) {
  236. console.log("data is viisible!");
  237. this.click('li[data-id="3200002"]');
  238. client.pause(1000);
  239.  
  240. firstCategoryClicked = true;
  241. }
  242.  
  243.  
  244. if(client.isVisible('li[data-id="3200002"] .nav-cont') && client.isVisible('li[data-id="3200291"]')) {
  245. console.log("3200291 is viisible!");
  246. this.click('li[data-id="3200291"]');
  247. client.pause(1000);
  248. }
  249.  
  250. if (client.isExisting('#title_ctnr') && client.isVisible('#title_ctnr')) {
  251. console.log("title is visible");
  252. }
  253.  
  254.  
  255.  
  256. client.isExisting('input[name="Title"]').then(isExisting => {
  257. if (isExisting === true) {
  258. client.setValue('input[name="Title"]', add_title);
  259. console.log("Setting title");
  260. }
  261. });
  262.  
  263.  
  264. client.isExisting('#pictures').then(isExisting => {
  265. if (isExisting === true) {
  266.  
  267. var toUpload = path.join(__dirname, '..', '..', 'icon-image-128.png');
  268. var toUpload = "C:\\Users\\michal\\Downloads\\tokchuju\\garage_attachment_zpscb730d13.jpg";
  269. console.log(toUpload);
  270.  
  271. this.chooseFile('#pictures', toUpload);
  272. clearInterval(tmpInterval);
  273. }
  274. });
  275.  
  276.  
  277. },10000);
  278.  
  279.  
  280. this.waitForVisible('li[data-id="3200002"]').then(function (visible) {
  281. console.log("wojewodztwa visible");
  282. this.pause(500).click('li[data-id="3200002"]').pause(500);
  283.  
  284. //this.click('li[data-id="3200311"]').pause(500);
  285.  
  286. //this.url("https://www.gumtree.pl/my/profile.html");
  287.  
  288.  
  289.  
  290.  
  291. });
  292.  
  293. console.log('sleep');
  294. client.pause(10000);
  295. this.pause(500).click('li[data-id="3200002"]').pause(500);
  296. });
  297.  
  298.  
  299.  
  300.  
  301. })
  302.  
  303.  
  304. })
  305. }
  306.  
  307.  
  308. }
  309.  
  310.  
  311.  
  312. function GumtreeInit() {
  313.  
  314.  
  315. var user = {username:"", password:""};
  316.  
  317. Gumtree.login(user).then().then(Gumtree.add_ad).then();
  318.  
  319. return;
  320.  
  321. exit;
  322. }
  323.  
  324.  
  325.  
  326.  
  327. async function asyncTimeout(time) {
  328. return new Promise((resolve, reject) => {
  329. setTimeout(()=>{
  330. resolve();
  331. }, time);
  332. })
  333. }
  334.  
  335. async function letsgo(ad) {
  336.  
  337. await client
  338. .url("https://www.gumtree.pl/post.html");
  339.  
  340. await client.click('[data-id="'+ad.gumtree_category_1_level+'"]');
  341.  
  342. while(true){
  343.  
  344. try {
  345. const existing = await
  346. client.isExisting('[data-id="'+ad.gumtree_category_2_level+'"]');
  347. console.log("existing: " + existing);
  348. if (existing) {
  349. const visible = await
  350. client.isVisible('[data-id="'+ad.gumtree_category_2_level+'"]');
  351. console.log("visible: " + visible);
  352.  
  353. if (visible) {
  354. await
  355. client.click('[data-id="'+ad.gumtree_category_2_level+'"]');
  356. break;
  357. }
  358. }
  359. }catch(Exception){
  360.  
  361. }
  362.  
  363. await asyncTimeout(1000);
  364. }
  365.  
  366. console.log("klikniete kategorie ");
  367.  
  368. while(true){
  369.  
  370. const existing = await client.isExisting('[data-id="'+ad.gumtree_region_1_level+'"]');
  371. console.log("existing: " + existing);
  372. if (existing) {
  373. const visible = await client.isVisible('[data-id="'+ad.gumtree_region_1_level+'"]');
  374. console.log("visible: " + visible);
  375.  
  376. if (visible) {
  377. await client.click('[data-id="'+ad.gumtree_region_1_level+'"]');
  378. break;
  379. }
  380. }
  381.  
  382. await asyncTimeout(1000);
  383. }
  384.  
  385. while(true){
  386. const existing = await client.isExisting('[data-id="'+ad.gumtree_region_2_level+'"]');
  387. if (existing) {
  388. const visible = await client.isVisible('[data-id="'+ad.gumtree_region_2_level+'"]');
  389. if (visible) {
  390. await client.click('[data-id="'+ad.gumtree_region_2_level+'"]');
  391. break;
  392. }
  393. }
  394.  
  395. await asyncTimeout(1000);
  396. }
  397.  
  398. console.log("region wybrany");
  399. while(true){
  400. const existing = await client.isExisting('input[name="Title"]');
  401. if (existing) {
  402. const visible = await client.isVisible('input[name="Title"]');
  403. if (visible) {
  404. await client.setValue('input[name="Title"]', ad.title);
  405. break;
  406. }
  407. }
  408.  
  409. await asyncTimeout(1000);
  410. }
  411.  
  412. if(ad.for_rent_by){
  413.  
  414.  
  415.  
  416.  
  417.  
  418. }
  419.  
  420. console.log("Tytul wpisany");
  421.  
  422. while(true){
  423. const existing = await client.isExisting('#description-frame');
  424. if (existing) {
  425. const visible = await client.isVisible('#description-frame');
  426. if (visible) {
  427. await client.click('#description-frame');
  428. await client.keys(ad.description);
  429. break;
  430. }
  431. }
  432.  
  433. await asyncTimeout(1000);
  434. }
  435.  
  436. console.log("opis wpisany");
  437.  
  438.  
  439. while(true){
  440. const existing = await client.isExisting('select[name="DwellingType"]');
  441. if (existing) {
  442. const visible = await client.isVisible('select[name="DwellingType"]');
  443. if (visible) {
  444. await client.selectByValue('select[name="DwellingType"]', ad.dwelling_type);
  445. break;
  446. }
  447. }
  448.  
  449. await asyncTimeout(1000);
  450. }
  451.  
  452. console.log("Rodzaj nieruchomosci wybrany");
  453.  
  454. }
  455.  
  456.  
  457. async function zrobpetleresult(result) {
  458. await letsgo(result);
  459.  
  460. }
  461.  
  462. async function inicjuj(){
  463. client.init().then(function (k) {
  464.  
  465. var url = "http://localhost:8000/api/ads";
  466.  
  467. request({
  468. url: url,
  469. json: true
  470. }, function (error, response, body) {
  471.  
  472. if (!error && response.statusCode === 200) {
  473.  
  474. for (let result of body)
  475. {
  476. console.log(result);
  477.  
  478. await zrobpetleresult(result);
  479. //break;
  480. }
  481. ;
  482.  
  483. }
  484. });
  485. return;
  486. });
  487. }
  488. inicjuj();
  489.  
  490.  
  491. return;
  492.  
  493.  
  494. letsgo();
  495.  
  496. return;
  497.  
  498. GumtreeInit();
  499.  
  500. return;
  501.  
  502. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement