first commit
This commit is contained in:
69
lib/cookies.js
Normal file
69
lib/cookies.js
Normal file
@@ -0,0 +1,69 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const request =require('request')
|
||||
const Headers = require('./headers')
|
||||
|
||||
// files & endpoints
|
||||
const RAW_COOKIE_FILE = 'raw_jar.txt'
|
||||
const JSON_COOKIE_FILE = 'cookies.json'
|
||||
const TEST_ENDPOINT = 'https://layla.amazon.de/api/devices/device'
|
||||
|
||||
// Returns the full raw cookies path
|
||||
const getRawCookiePath = data_dir => path.join(data_dir, RAW_COOKIE_FILE)
|
||||
// Checks if the JSON cookie file exists
|
||||
const getJsonCookiePath = data_dir => path.join(data_dir, JSON_COOKIE_FILE)
|
||||
// Checks if the raw cookie file exists
|
||||
const checkRawCookiesExist = data_dir => fs.existsSync(getRawCookiePath(data_dir))
|
||||
// Checks if the JSON cookie file exists
|
||||
const checkJsonCookiesExist = data_dir => fs.existsSync(getJsonCookiePath(data_dir))
|
||||
// Checks if the raw cookie file exists
|
||||
const deleteRawCookies = data_dir => fs.removeSync(getRawCookiePath(data_dir))
|
||||
// Checks if the JSON cookie file exists
|
||||
const deleteJsonCookies = data_dir => fs.removeSync(getJsonCookiePath(data_dir))
|
||||
// Returns the cookies as a header compatible string
|
||||
const transformCookiesForHeader = json_cookies => {
|
||||
let string_cookies = ''
|
||||
json_cookies.forEach(cookie => {
|
||||
Object.keys(cookie).forEach(key => {
|
||||
if (key == 'name') return
|
||||
string_cookies += key == 'value' ? cookie.name + '=' + cookie.value : key + '=' + cookie[key]
|
||||
string_cookies += ';'
|
||||
})
|
||||
})
|
||||
return string_cookies
|
||||
}
|
||||
// Validate the cookie
|
||||
const validateCookie = (data_dir) => {
|
||||
const json_cookies = require(path.join(data_dir, JSON_COOKIE_FILE))
|
||||
const string_cookies = transformCookiesForHeader(json_cookies)
|
||||
const headers = Headers.getFetchHeaders(string_cookies)
|
||||
return new Promise((resolve, reject) => {
|
||||
request.get({headers, url: TEST_ENDPOINT}, (error, response, body) => {
|
||||
try {
|
||||
let contents = JSON.parse(body)
|
||||
if (contents.devices) {
|
||||
resolve([true, contents])
|
||||
} else {
|
||||
reject([false, null])
|
||||
}
|
||||
} catch (e) {
|
||||
reject([false, body])
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getRawCookiePath,
|
||||
getJsonCookiePath,
|
||||
checkRawCookiesExist,
|
||||
checkJsonCookiesExist,
|
||||
deleteRawCookies,
|
||||
deleteJsonCookies,
|
||||
validateCookie,
|
||||
transformCookiesForHeader,
|
||||
RAW_COOKIE_FILE,
|
||||
JSON_COOKIE_FILE,
|
||||
}
|
||||
Reference in New Issue
Block a user