Skip to content

Commit

Permalink
Merge branch '2.1.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
gegehprast committed Jan 18, 2020
2 parents d128728 + cf91a7c commit a38ba43
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 25 deletions.
61 changes: 38 additions & 23 deletions Browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,22 @@ class Browser {
async newOptimizedPage() {
const page = await this.browser.newPage()
page.setDefaultTimeout(30000)
await page.setRequestInterception(true)

page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return page
return await this.optimizePage(page)
}

/**
* Create new page with different browser context
* to support multiple sessions.
* to support multiple sessions. The returned page will be optimized.
* https://github.com/GoogleChrome/puppeteer/issues/85
*/
async newPageWithNewContext() {
const { browserContextId } = await this.browser._connection.send('Target.createBrowserContext')
const page = await this.browser._createPageInContext(browserContextId)
page.browserContextId = browserContextId
page.setDefaultTimeout(60000)

page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return page
page.setDefaultTimeout(30000)

return await this.optimizePage(page)
}

/**
Expand All @@ -68,8 +51,26 @@ class Browser {
const pageTarget = page.target()
const newTarget = await this.browser.waitForTarget(target => target.opener() === pageTarget)
const newPage = await newTarget.page()
newPage.setDefaultTimeout(30000)

return await this.optimizePage(newPage)
}

/**
* Optimize a page.
*/
async optimizePage(page) {
await page.setRequestInterception(true)

page.on('request', (req) => {
if (req.resourceType() == 'stylesheet' || req.resourceType() == 'font' || req.resourceType() == 'image') {
req.abort()
} else {
req.continue()
}
})

return newPage
return page
}

/**
Expand Down Expand Up @@ -121,6 +122,20 @@ class Browser {
async getPlainProperty(element, property) {
return await element.getProperty(property).then(x => x.jsonValue())
}

/**
* Get a cookie object of page.
*
* @param {Object} page Browser page
* @param {String} name Name of the cookie to select
*/
async getCookie(page, name) {
const cookies = await page.cookies()

const cookie = cookies.filter(cookie => cookie.name == name)

return cookie[0]
}
}

module.exports = new Browser
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "shallty",
"version": "2.1.0",
"version": "2.1.3",
"description": "Shallty adalah aplikasi untuk meng-crawl situs fastsub/fanshare Indonesia. Tujuan utamanya adalah untuk melewati berbagai halaman redirect dan mengambil tautan unduh aslinya. Saat ini Shallty telah mendukung crawling untuk Meownime, Samehadaku, Neonime, dan Oploverz. https://shallty.kyuun.id",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 11 additions & 1 deletion shortlinks/Hightech.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,21 @@ class Hightech {
}
}

const page = await Browser.newOptimizedPage()
const page = await Browser.newPageWithNewContext()

try {
await page.goto(link)

const xyzklCookie = await Browser.getCookie(page, 'xyzkl')

if (xyzklCookie && xyzklCookie.value) {
await page.close()

return {
url: Util.base64Decode(xyzklCookie.value)
}
}

await Util.sleep(8000)
await page.waitForSelector('a[href="#generate"]')
await page.click('a[href="#generate"]')
Expand Down
23 changes: 23 additions & 0 deletions test/integration/shortlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ describe('shortlink', function () {
})
})

describe('hightech', function () {
it('should return 200 and a string of url', function (done) {
this.timeout(60000)
supertest(app).get('/api/shortlink?link=https%3A%2F%2Fhightech.web.id%2Fko82jkl9%2F%3Fxyzkl%3DaHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL3VjP2lkPTFheVhtZlcwWV9VZjRGMDhqcjVjTDl0NDhDVnBmUlJ2Xw%3D%3D')
.expect(200)
.expect(function (res) {
expect(res.body.status).to.equal(200)
expect(res.body.message).to.equal('Success')
expect(res.body.data).to.be.an('object')
expect(res.body.data).to.has.property('url')
expect(res.body.data.url).to.be.a('string')
expect(res.body.data.url).to.include('google')
})
.end(function (err, res) {
if (err) {
console.log(res.body)
return done(err)
}
done()
})
})
})

describe('anjay', function () {
it('should return 200 and a string of url', function (done) {
this.timeout(60000)
Expand Down
13 changes: 13 additions & 0 deletions test/unit/shortlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ describe('shortlink', function () {
})
})

describe('hightech', function () {
it('should return an object which has a string url property', async function () {
this.timeout(60000)
await Browser.init()
const data = await Shortlink.parse('https%3A%2F%2Fhightech.web.id%2Fko82jkl9%2F%3Fxyzkl%3DaHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL3VjP2lkPTFheVhtZlcwWV9VZjRGMDhqcjVjTDl0NDhDVnBmUlJ2Xw%3D%3D')

expect(data).to.be.an('object')
expect(data).to.has.property('url')
expect(data.url).to.be.a('string')
expect(data.url).to.include('google')
})
})

describe('anjay', function () {
it('should return an object which has a string url property', async function () {
this.timeout(60000)
Expand Down

0 comments on commit a38ba43

Please sign in to comment.