Skip to content

Instantly share code, notes, and snippets.

@Deviad
Created April 1, 2017 10:47
Show Gist options
  • Save Deviad/d32209440be74d14ec39976e536844a9 to your computer and use it in GitHub Desktop.
Save Deviad/d32209440be74d14ec39976e536844a9 to your computer and use it in GitHub Desktop.
Issue with createCookie it returns undefined as if it cannot find the file cookies.json.
/**
* Created by Davide Pugliese on 26/02/17.
*/
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/from";
let request = require('request');
import { userObservable } from "./user.service";
import {first} from "rxjs/operator/first";
import {last} from "rxjs/operator/last";
import * as path from 'path';
let FileCookieStore = require('tough-cookie-filestore');
// let csrfLogin = require('csrf-login');
const fs = require('fs');
const Promise = require('promise');
let readFile = Promise.denodeify(require('fs').readFile);
const efs = require('extfs');
class LinkedinDataService {
constructor() {
}
getCookie = function () {
efs.isEmpty(path.join(__dirname, '..', '../dist/cookies/cookies.json'), function (empty) {
//this is executed only if the file is empty, this avoids having the same file written multiple times
//hence a file that contains invalid JSON.
if (empty) {
let j = request.jar(new FileCookieStore(path.join(__dirname, '..', '../dist/cookies/cookies.json')));
request = request.defaults({ jar : j });
console.log(empty);
return request('https://www.linkedin.com', function() {
request('https://www.linkedin.com/uas/login-submit');
});
}
});
};
createCookieObject (callback) {
callback();
return readFile(path.join(__dirname, '..', '../dist/cookies/cookies.json'), 'utf8')
.then(JSON.parse).nodeify(callback);
}
getData<T>(firstname:string, lastname: string) {
// console.log('First Name ' + firstname + ' \nLast Name ' + lastname);
// NOTE - currently the 'cookies.json' file must already exist!
// let j = request.jar(new FileCookieStore(path.join(__dirname, '..', '../dist/cookies/cookies.json')));
// request = request.defaults({ jar : j });
// request('http://www.google.com', function() {
// request('http://images.google.com');
// });
let createCookie = this.createCookieObject(() => {
this.getCookie();
}
);
Observable.from(createCookie);
return userObservable.subscribe(
(x) => JSON.stringify(x)
);
}
}
export { LinkedinDataService };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment