Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var request = require('request');
- var querystring = require('querystring');
- var cheerio = require('cheerio');
- var courses = [];
- var async = require('async');
- var form = {
- username: '3500190',
- password: '1093866620'
- };
- var formData = querystring.stringify(form);
- var contentLength = formData.length;
- var cookieJar = request.jar();
- var name ='';
- var Total_Absence = '';
- var myUrls=[];
- var obj ={}; //important
- var objects = [];
- var student ={
- name:'',
- courses:[],
- links:[]
- };
- exports.handler = function() {
- async.waterfall([
- function(callback) {
- request({
- headers: {
- 'Content-Length': contentLength,
- 'Content-Type': 'application/x-www-form-urlencoded'
- },
- followAllRedirects: true,
- uri: 'http://lms.yic.edu.sa/moodle/login/',
- body: formData,
- method: 'POST',
- jar: cookieJar
- },
- function (err, res, body) {
- if(err) {
- console.log(err);
- callback(true);
- return;
- }
- console.log('first api');
- $ = cheerio.load(body);
- $('h4','#region-main').each(function(){
- var course = this.children[0].data;
- student.courses.push(course);
- var url = this.parent.parent.children[1].children[0].children[0].children[0].children[1].children[0].attribs.href+'&tab=attendance&att_week=1';
- student.links.push(url);
- });//end of each course loop
- name = $('a.dropdown-toggle','#essentialnavbar').text();
- student.name = name;
- return callback(false, student); //callback(err,data);
- }); //end of request
- },
- function(argv1,callback) {
- for (i in argv1.links) {
- request({uri:argv1.links[i],
- jar: cookieJar,
- followAllRedirects: true,
- method: 'GET'},(err,res,body)=>{
- console.log('second api');
- if(err) {console.log(err);callback(true);return; }
- $ = cheerio.load(body);
- var course = $('#page-navbar').find('span');
- var courseName =course[5].children[0].children[0].children[0].data
- $('tr','#region-main').each(function(){
- Total_Absence=this.children[3].children[0].data;
- if(Total_Absence!=null){
- if(Total_Absence.indexOf('Hours')!=-1){
- for (var i=0; i < argv1.courses.length; i++) {
- if (argv1.courses[i].includes(courseName.split(' ').slice(0,1))) {
- obj = [argv1.courses[i],Total_Absence];
- objects.push(obj);
- //This is the most important obj in class
- }
- }
- }
- }
- });//end of each absence loop
- });//end of request loop
- }//end of links loop
- return callback(false, objects);
- }
- ],
- function (err, results) {
- if(err) { console.log(err); res.send(500,"Server Error"); return; }
- console.log({api1:results[0], api2:results[1]});
- }
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement