Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. startFetch: function () {
  2.         _chromeStorage.get(CONST.STORE_QUERY_URL, function (items) {
  3.             var trIfEmptyQuery = "<tr style='background-attachment: fixed' class='tr-when-empty'><td colspan='4'>Trenutno ni nastavljen noben filter iskanja.... <a href='http://www.avto.net/_MOTO/'>(-nastavi-)</a></td></tr>"
  4.             if (items != undefined && Object.getOwnPropertyNames(items).length >= 1 && items.queryUrl != undefined) {
  5.                 var queryUrl = items.queryUrl;
  6.                 var keyObject = {};
  7.                 keyObject[CONST.STORE_LAST_FETCHED] = {dateTime: new Date().toString()};
  8.                 _chromeStorage.set(keyObject);
  9.  
  10.                 var promis = new Promise(function(resolve){
  11.                     resolve(AVTONET.fetchAllImportIO(queryUrl));
  12.                 });
  13.                 promis.then(function(a){
  14.                     console.log(a)
  15.                 })
  16.             }
  17.         });
  18.     },
  19.  
  20.     //fetchAllYQL: function(queryUrl){
  21.     //    var jsonResults = [];
  22.     //    $.getJSON("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(queryUrl)+"%22%20and%20xpath%3D%22%2F%2Fdiv%5Bcontains(concat('%20'%2C%20%40class%2C%20'%20')%2C%20'%20ResultsAd%20')%5D%22&format=json&callback=", function(r){
  23.     //        console.log("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22"+encodeURIComponent(queryUrl)+"%22%20and%20xpath%3D%22%2F%2Fdiv%5Bcontains(concat('%20'%2C%20%40class%2C%20'%20')%2C%20'%20ResultsAd%20')%5D%22&format=json&callback=")
  24.     //        $.each(r.query.results, function(){
  25.     //            jsonResults.push(this)
  26.     //        });
  27.     //
  28.     //        _chromeStorage.get("storedId", function(item){
  29.     //            var setPromises = [];
  30.     //            var storedId = item;
  31.     //            if(storedId == undefined || Object.getOwnPropertyNames(storedId).length < 1){
  32.     //                storedId = 0;
  33.     //            }else{
  34.     //                storedId = item.storedId
  35.     //            }
  36.     //            console.log(jsonResults);
  37.     //            var i;
  38.     //            for(i = 0; i < jsonResults[0].length; i++){
  39.     //                var jsonObj = jsonResults[0][i];
  40.     //                var url = jsonObj.div[0].div;
  41.     //                let photo;
  42.     //                if(url.length > 1){
  43.     //                    photo = url[0].a.img.src;
  44.     //                    url = url[0].a.href;
  45.     //                }else{
  46.     //                    photo = url.a.img.src;
  47.     //                    url = url.a.href;
  48.     //                }
  49.     //                let uri = encodeURI("http://www.avto.net/_MOTO"+url.substring(2, url.length));
  50.     //                let title = jsonObj.div[1].a.content;
  51.     //                let price = jsonObj.div[2].div[0].content.trim();
  52.     //
  53.     //                setPromises.push(new Promise(function(resolve){
  54.     //                    UTILS.adExists(uri, function(exits, obj){
  55.     //                        if(!exits){
  56.     //                            var keyObject = {};
  57.     //                            keyObject["url"+storedId] = {url: uri, title: title, price:price, newPrice:price, pic:photo, removed: false, pinned: false, note:""};
  58.     //                            _chromeStorage.set(keyObject);
  59.     //                            console.log("persisting "+uri+"|"+title+"|"+price+" to key url"+storedId);
  60.     //                            storedId++;
  61.     //                        }else{
  62.     //                            if(obj.price != price){
  63.     //                                var keyObject = {};
  64.     //                                if(obj.newPrice != undefined){
  65.     //                                    obj.price = obj.newPrice;
  66.     //                                }
  67.     //                                obj.newPrice = price;
  68.     //
  69.     //                                keyObject[obj.key] = obj;
  70.     //                                _chromeStorage.set(keyObject);
  71.     //                                console.log("price has changed from "+obj.price+"to "+price);
  72.     //
  73.     //                            }else{
  74.     //                                console.log("uri: "+uri+" already exists, not persisting");
  75.     //                            }
  76.     //                        }
  77.     //                        resolve();
  78.     //                    });
  79.     //                }))
  80.     //
  81.     //            }
  82.     //
  83.     //            Promise.all(setPromises).then(function() {
  84.     //                var storedObj = {};
  85.     //                storedObj["storedId"] = storedId;
  86.     //                _chromeStorage.set(storedObj);
  87.     //
  88.     //                _chromeStorage.get("storedId", function(item){
  89.     //                    console.log("stored id after complete: ");
  90.     //                    console.log(item);
  91.     //                });
  92.     //            });
  93.     //
  94.     //
  95.     //
  96.     //        });
  97.     //    });
  98.     //},
  99.  
  100.     fetchAllImportIO: function (queryUrl, page) {
  101.         queryUrl = queryUrl + CONST.URL_PARAM_PAGE + page;
  102.  
  103.         var allDonePromises = [];
  104.  
  105.         var jsonResults = [];
  106.         var rtrnUris = [];
  107.         var ioimporturl = CONST.FETCH_IOIMPORT_URL_P1 + encodeURIComponent(queryUrl) + CONST.FETCH_IOIMPORT_URL_P2;
  108.         $.getJSON(ioimporturl, function (r) {
  109.             console.log(r);
  110.             $.each(r.tables[0].results, function () {
  111.                 jsonResults.push(this)
  112.             });
  113.  
  114.             allDonePromises.push(new Promise(function(resolve){
  115.                 _chromeStorage.get(CONST.STORE_STORED_ID, function (item) {
  116.                     var setPromises = [];
  117.                     var storedId = item;
  118.                     if (storedId == undefined || Object.getOwnPropertyNames(storedId).length < 1) {
  119.                         storedId = 0;
  120.                     } else {
  121.                         storedId = item.storedId
  122.                     }
  123.                     console.log(jsonResults);
  124.                     var i;
  125.                     for (i = 0; i < jsonResults.length; i++) {
  126.                         var jsonObj = jsonResults[i];
  127.  
  128.                         var url = encodeURI(jsonObj[CONST.IOIMPORT_PARSE_URL]);
  129.                         let uri = encodeURI(CONST.AVTONET_MOTO_PREFIX + url.substring(2, url.length));
  130.                         rtrnUris.push(uri);
  131.                         let title = jsonObj[CONST.IOIMPORT_PARSE_TITLE];
  132.                         let price = jsonObj[CONST.IOIMPORT_PARSE_PRICE];
  133.                         let photo = jsonObj[CONST.IOIMPORT_PARSE_PHOTO];
  134.  
  135.                         if(price == undefined){
  136.                             price = "Pokličite za ceno"
  137.                         }
  138.  
  139.                         setPromises.push(new Promise(function (resolve) {
  140.                             UTILS.adExists(uri, function (exits, obj) {
  141.                                 if (!exits) {
  142.                                     var keyObject = {};
  143.                                     keyObject["url" + storedId] = {
  144.                                         url: uri.toLowerCase(),
  145.                                         title: title,
  146.                                         price: price,
  147.                                         pic: photo,
  148.                                         removed: false,
  149.                                         pinned: false,
  150.                                         note: "",
  151.                                         key: "url" + storedId
  152.                                     };
  153.                                     _chromeStorage.set(keyObject);
  154.                                     console.log("persisting " + uri + "|" + title + "|" + price + " to key url" + storedId);
  155.                                     chrome.runtime.sendMessage(CONST.MSG_ICON_NEW);
  156.                                     storedId++;
  157.                                 } else {
  158.                                     if (obj.price != price) {
  159.                                         var keyObject = {};
  160.                                         if (obj.newPrice != undefined) {
  161.                                             obj.price = obj.newPrice;
  162.                                         }
  163.                                         obj.newPrice = price;
  164.  
  165.                                         keyObject[obj.key] = obj;
  166.                                         _chromeStorage.set(keyObject);
  167.                                         console.log("price has changed from " + obj.price + "to " + price);
  168.                                         chrome.runtime.sendMessage(CONST.MSG_ICON_NEW);
  169.  
  170.                                     } else {
  171.                                         console.log("uri: " + uri + " already exists, not persisting");
  172.                                     }
  173.                                 }
  174.                                 resolve();
  175.                             });
  176.                         }));
  177.  
  178.                     }
  179.  
  180.                     Promise.all(setPromises).then(function () {
  181.                         var storedObj = {};
  182.                         storedObj[CONST.STORE_STORED_ID] = storedId;
  183.                         _chromeStorage.set(storedObj);
  184.                     });
  185.  
  186.                 resolve();
  187.                 });
  188.  
  189.             }));
  190.  
  191.             Promise.all(allDonePromises).then(function(){
  192.                 return rtrnUris;
  193.             });
  194.  
  195.  
  196.         });
  197.  
  198.  
  199.  
  200.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement