-
Notifications
You must be signed in to change notification settings - Fork 6
Description
Hello,
I did not have time to test it on other websites, I gave you @bahmutov access to my private repo.
I will publish publicly my work later on with more features and prolly also an interface made with Vue 2.
I created my own User Interface so that I can replace the .env config file with the data coming from an external API or from a DB connection.
However, I tested the code also with the config file with the default name.
Thank you for your help.
All the best,
Davide
The error I have using this on Linkedin is:
node:26190) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Could not find login form
This is the important portion of the code in regard to csrf-login.
----user.service.ts-------------
/**
* Created by Davide Pugliese on 31/03/17.
*/
import {Observable, Subscribable} from "rxjs/Observable";
import "rxjs/add/observable/from";
import "rxjs/add/observable/fromPromise";
import "rxjs/add/observable/of";
import "rxjs/add/operator/map";
import "rxjs/add/observable/defer";
import * as path from 'path';
import {Observer, Subject} from "rxjs";
// const request = require('request');
const fs = require('fs');
const Promise = require('promise');
interface IUser {
host: String;
username: String;
password: String;
loginFormSelector: String;
loginUsernameField: String;
loginPasswordField: String;
tokenFieldName: String;
loginPath: String;
}
class User <Observable> implements IUser {
public host;
public username;
public password;
public loginFormSelector;
public loginUsernameField;
public loginPasswordField;
public tokenFieldName;
public loginPath;
}
class UserFactory {
build() {
//We create a promise inside a factory class in order to decouple this precise "user feed" from the rest of the code.
//In other words the code is ready for multiple "user feeds" as for example an API without having to refactor
//later on.
let readFile = Promise.denodeify(require('fs').readFile);
return readFile(path.join(__dirname, '..', '../.env.json'), 'utf8')
.then(
(x) => {
let a = JSON.parse(x);
let user = new User();
user.host = a.host;
user.username = a.username;
user.password = a.password;
user.loginFormSelector = a.loginFormSelector;
user.loginUsernameField = a.loginUsernameField;
user.loginPasswordField = a.loginPasswordField;
user.tokenFieldName = a.tokenFieldName;
user.loginPath = a.loginPath;
console.log(JSON.stringify(user));
return user;
}
);
}
}
let user = new UserFactory();
// // var user = Observable.of(new User()).map(o => JSON.stringify(o));
export let userObservable: Observable<any> = Observable.from(user.build());
// userObservable.subscribe(x => console.log(JSON.stringify(x)));
--------linkedin.data.service.ts---------------------
/**
* Created by Davide Pugliese on 31/03/17.
*/
import { Observable } from "rxjs/Observable";
import "rxjs/add/observable/from";
import "rxjs/add/observable/fromPromise";
import "rxjs/add/operator/do";
import "rxjs/add/operator/map";
let request = require('request');
import { userObservable } from "./user.service";
import * as path from 'path';
let FileCookieStore = require('tough-cookie-filestore');
let csrfLogin = require('csrf-login');
// const fs = require('fs');
// const Promise = require("bluebird");
// let fs = Promise.promisifyAll(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): Promise<any> {
callback();
return readFile(path.join(__dirname, '..', '../dist/cookies/cookies.json'), 'utf8')
.then(
(x) => {
let a = JSON.parse(x);
return a;
}
);
}
getData<T>(firstname:string, lastname: string) {
let createCookie: Promise<any> = this.createCookieObject(() => {
this.getCookie();
}
);
// let cookieObservable: Observable<any> = Observable.fromPromise(createCookie);
//
// return cookieObservable.subscribe(
// x => console.log(JSON.stringify(x))
// );
return userObservable.subscribe(
(user) => {
csrfLogin(user)
.then(function (info) {
console.log(Object.keys(info));
// [ 'request', 'requestAsync', 'response', 'config', 'jar' ]
})
}
);
}
}
export { LinkedinDataService };
--------.env.json------
{
"host": "https://www.linkedin.com",
"username": "Linkedin Username",
"password": "Linkedin Password",
"loginFormSelector": "class='login-form'",
"loginUsernameField": "session_key",
"loginPasswordField": "session_password",
"tokenFieldName": "loginCsrfParam",
"loginPath": "/uas/login-submit/"
}