-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: cookie storage permission issues on linux
- Loading branch information
1 parent
ce9b7cc
commit 140fc69
Showing
3 changed files
with
89 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const fs = require('fs'); | ||
const Client = require('instagram-private-api').V1; | ||
|
||
const canUseFileStorage = () => { | ||
try { | ||
fs.accessSync(`${__dirname}/cookies/`, fs.W_OK); | ||
return true | ||
} catch (error) { | ||
return false | ||
} | ||
} | ||
|
||
const guessUsername = () => { | ||
let username; | ||
if (canUseFileStorage()) { | ||
const files = fs.readdirSync(`${__dirname}/cookies`); | ||
if (files.length && files[0].endsWith('.json')) { | ||
username = files[0].slice(0, -5); | ||
} | ||
} | ||
return username; | ||
} | ||
|
||
const getCookieStorage = (filePath) => { | ||
let storage; | ||
let username; | ||
|
||
if (canUseFileStorage()) { | ||
if (!filePath && (username = guessUsername())) { | ||
filePath = `${__dirname}/cookies/${username}.json` | ||
} | ||
|
||
if (filePath) storage = new Client.CookieFileStorage(filePath); | ||
} else { | ||
storage = new Client.CookieMemoryStorage(); | ||
} | ||
|
||
return storage; | ||
} | ||
|
||
const clearCookieFiles = () => { | ||
// delete all session storage | ||
if (canUseFileStorage()) { | ||
fs.readdirSync(`${__dirname}/cookies`).forEach((filename) => { | ||
fs.unlinkSync(`${__dirname}/cookies/${filename}`); | ||
}) | ||
} | ||
} | ||
|
||
const getDevice = (username) => { | ||
let device; | ||
username = username || guessUsername(); | ||
if (username) device = new Client.Device(username); | ||
return device; | ||
} | ||
|
||
module.exports = { | ||
canUseFileStorage, guessUsername, | ||
getCookieStorage, clearCookieFiles, getDevice | ||
} |