-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·91 lines (82 loc) · 2.47 KB
/
index.js
File metadata and controls
executable file
·91 lines (82 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"use strict";
import Assert from 'assert-js';
import LoginPage from './LoginPage';
import UserPage from './UserPage';
import phantom from 'phantom';
import UserBehaviour from './UserBehaviour';
import Logger from './Logger';
import fs from 'fs';
import Q from 'q';
import PhantomScraper from './PhantomScraper';
const config = JSON.parse(fs.readFileSync('../config.json', 'utf8'));
const handles = JSON.parse(fs.readFileSync('../handles.json', 'utf8'));
let chain = Q.when();
createPageAndLogin()
.then((page) => {
let handleCurrentIndex = 0;
for (var i = 0; i < handles.names.length; i++) {
chain = chain.then(() => {
let handle = handles.names[handleCurrentIndex];
handleCurrentIndex++;
return followUser(page, handle);
});
}
//Add final promise to close the page and exit phantom
chain = chain.then(() => {
return new Promise((resolve, reject) => {
page.quit();
resolve();
});
});
})
.catch((error) => {
console.log(error);
error.page.quit();
});
/**
* createPageAndLogin Method to login
* @return {Promise}
*/
function createPageAndLogin() {
return phantom.create()
.then(instance => {
let loginPage = new LoginPage(config, new PhantomScraper(instance), new UserBehaviour(config.minWaitTime, config.maxWaitTime), new Logger(config.logFile));
return loginPage.open(config.userAgent);
})
.then((page) => {
return page.fillUsername(config.username);
})
.then((page) => {
return page.fillPassword(config.password);
})
.then((page) => {
return page.clickSubmit();
})
.then((page) => {
return page.getLoggedInScreenshot();
})
.then((page) => {
Assert.instanceOf(page, UserPage);
return page
})
.catch((error) => {
console.log(error);
error.page.quit();
});
}
function followUser(page, handle) {
Assert.instanceOf(page, UserPage);
Assert.string(handle);
return page.mandatoryWait()
.then((page) => {
console.log(page);
return page.goToUser(handle);
})
.then((page) => {
return page.follow();
})
.catch((error) => {
console.log(error);
page.quit();
});
}