如何将后台作业转换为类似功能?

时间:2014-04-26 10:20:48

标签: javascript android parse-platform

我正在制作一个新闻阅读器应用程序,并使用Parse.com后台作业从报纸的RSS源收集链接。我使用xmlreader.js和sax.js来解析httpResponse并使用saveAll和beforeSave,定期更新数据浏览器中的类。

我有多个报纸,多个类别总共超过30对,(我将不得不包括更多的对,因为我想包括地区报纸)。直到现在我正在使用一份报纸和一个类别 - 印度教,体育类别;它现在工作正常。我认为复制这两个功能并创造就业机会并不高效。

因此,我想询问我是否可以将这些作业和之前的作业转换成某种功能,以便我可以直接传入报纸类别对类名或其网址来自动完成这些工作。

完整代码 - main.js

工作 -

Parse.Cloud.job("job_hindu_sports", function (request, response) {
return Parse.Cloud.httpRequest({
    url: 'http://www.thehindu.com/sport/?service=rss'
}).then(function(httpResponse) {
    var someXml = httpResponse.text;
    xmlreader.read(someXml, function (err, res){
        if(err) {
            response.error("Error " +err);
            return console.log(err);
        }   

        var listArray = [];
        res.rss.channel.item.each(function (i, item){
            var hinduSports = new HinduSports(); //@startswithaj - this part
            hinduSports.set("link", item.link.text());
            hinduSports.set("title", item.title.text());
            hinduSports.set("pubDate", item.pubDate.text());
            //console.log("pubDate - "+ item.pubDate.text());
            listArray.push(hinduSports);
        });

        var promises = [];
        Parse.Object.saveAll(listArray, {
                success: function(objs) {
                    promises.push(objs);
                    console.log("SAVED ALL!");
                },
                error: function(error) { 
                    console.log("ERROR WHILE SAVING - "+error);
                }   
            });
        return Parse.Promise.when(promises);        

    });
}).then(function() {
        response.success("Saving completed successfully.");
        },function(error) {
        response.error("Uh oh, something went wrong.");
});
});

beforeSave -

Parse.Cloud.beforeSave("HinduSports", function(request, response) {
//console.log("in beforeSave");
var query = new Parse.Query(HinduSports);
var linkText = request.object.get("link")
var titleText = request.object.get("title");
query.equalTo("link", linkText);
query.first({
  success: function(object) {
    //console.log("in query");
    if (object) {
        //console.log("found");
        if(object.get('title')!==titleText){
            console.log("title not same");
            object.set("title", titleText);
            response.success();
        }
        else{
            console.log("title same");
            response.error();
        }
    } else {
        console.log("not found");
        response.success();
    }
  },
  error: function(error) {
    response.error();
  }
});
});

2 个答案:

答案 0 :(得分:1)

在您的工作代码中,您可以在数据存储区中查询要处理的所有URL,然后遍历请求每个URL的结果并将httpresponse传递给执行所有工作的函数

所以你会有(伪代码)

function getDataForNewspaper(id, url){
    return (function(id) {
        Parse.Cloud.httpRequest({
            url: url
        }).then(function(httpResponse){
           processDataForNewspaper(id, httpResponse)
        })
    })(id) //you need to have this in a closure so you can pass id to processDataFor...

function processDataforNewpaper(id, httpResponse){
  someXml = httpResponse.text
  //process your xml here 
}

Parse.Cloud.job("get_data_for_all_newspapers", function (request, response) {
    var query = new Parse.Query("Get all the newspapers").find{
      success: function(list){ 
         for each newspaper in list then 
              getDataForNewspaper(newspaper.id, newspaper.url)  
      }
    }
}

这不是最好的解释,但我希望这有帮助

答案 1 :(得分:0)

在@startswithaj的帮助下,我修改了我的代码以保存一个类中的所有文章。唯一剩下的就是添加beforeSave方法。但仍有问题。 saveAll有时只会完成。例如。我第一次运行代码并将其记录在日志中:

I2014-04-26T18:18:40.036Z] v93: Ran job job_get_data_for_all_newspapers with:
Input: {}
Result: Saving completed successfully.
I2014-04-26T18:18:40.926Z] Successfully retrieved 2
I2014-04-26T18:18:40.926Z] getData NEW & CAT ID - 1, 5 feedUrl http://www.thehindu.com/sport/?service=rss
I2014-04-26T18:18:40.927Z] getData NEW & CAT ID - 1, 4 feedUrl http://www.thehindu.com/news/national/?service=rss
I2014-04-26T18:18:40.927Z] promisesGetNP [object Object],[object Object]
I2014-04-26T18:18:41.479Z] processData NEW & CAT ID - 1, 5
I2014-04-26T18:18:41.622Z] listArray http://www.thehindu.com/sport/other-sports/mankirat-singh-sets-record/article5951540.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication
I2014-04-26T18:18:41.628Z] promises undefined
I2014-04-26T18:18:41.629Z] promisesGetData 
I2014-04-26T18:18:41.629Z] Done getData? 
I2014-04-26T18:18:42.082Z] processData NEW & CAT ID - 1, 4
I2014-04-26T18:18:42.311Z] listArray http://www.thehindu.com/news/national/muslim-women-entitled-to-maintenance-even-after-divorce-supreme-court/article5951562.ece?utm_source=RSS_Feed&utm_medium=RSS&utm_campaign=RSS_Syndication
I2014-04-26T18:18:42.324Z] promises undefined
I2014-04-26T18:18:42.324Z] promisesGetData 
I2014-04-26T18:18:42.324Z] Done getData? 
I2014-04-26T18:18:42.324Z] done job

第二次删除一些无用的console.log后我得到了这个。您可以看到在SAVED ALL!的{​​{1}}中调用success: function -

saveAll

我的新代码可以找到here。新代码从第150行开始。