diff --git a/igdm.js b/instagram.js similarity index 67% rename from igdm.js rename to instagram.js index 96b7882..f37de14 100644 --- a/igdm.js +++ b/instagram.js @@ -1,49 +1,34 @@ -const fs = require('fs'); const Client = require('instagram-private-api').V1; -let storage, device; +const utils = require('./utils'); -exports.checkAuth = function () { +exports.checkAuth = function (session) { return new Promise((resolve, reject) => { - let files = fs.readdirSync(`${__dirname}/cookies`); - - if (files.length && files[0].endsWith('.json')) { - let filename = files[0] - device = new Client.Device(filename.slice(0, -5)); // remove .json extension - storage = new Client.CookieFileStorage(`${__dirname}/cookies/${filename}`); - - const session = new Client.Session(device, storage); - // see if IG allows this session to make request - return session.getAccountId() - .then(() => resolve({ isLoggedIn: true, session })) - .catch(Client.Exceptions.CookieNotValidError, () => resolve({ isLoggedIn: false })) - } else { - resolve({ isLoggedIn: false }) + if (!session) { + const device = utils.getDevice(); + const storage = utils.getCookieStorage(); + if (!device || !storage) { + return resolve({ isLoggedIn: false }); + } + session = new Client.Session(device, storage); } - }) + + session.getAccountId() + .then(() => resolve({ isLoggedIn: true, session })) + .catch(Client.Exceptions.CookieNotValidError, () => resolve({ isLoggedIn: false })) + }); } exports.login = function (username, password) { - // delete all session storage - let files = fs.readdirSync(`${__dirname}/cookies`); - files.forEach((filename) => { - fs.unlinkSync(`${__dirname}/cookies/${filename}`); - }) - + utils.clearCookieFiles(); return new Promise((resolve, reject) => { - // create file - device = new Client.Device(username); - storage = new Client.CookieFileStorage(`${__dirname}/cookies/${username}.json`); - + const device = utils.getDevice(username); + const storage = utils.getCookieStorage(`${__dirname}/cookies/${username}.json`); Client.Session.create(device, storage, username, password).then(resolve).catch(reject) }) } exports.logout = function () { - // delete all session storage - let files = fs.readdirSync(`${__dirname}/cookies`); - files.forEach((filename) => { - fs.unlinkSync(`${__dirname}/cookies/${filename}`); - }) + utils.clearCookieFiles(); } exports.getChatList = function (session) { diff --git a/main.js b/main.js index 332cb94..431d2c8 100644 --- a/main.js +++ b/main.js @@ -5,7 +5,7 @@ const menuTemplate = require('./menutemplate'); const BrowserWindow = electron.BrowserWindow; const path = require('path'); const url = require('url'); -const igdm = require('./igdm'); +const instagram = require('./instagram'); const autoUpdater = require('./autoupdater'); const notifier = require('node-notifier'); let pollingInterval = 10000; @@ -39,7 +39,7 @@ function createWindow () { } mainWindow.setTitle('Desktop IG:dm') - igdm.checkAuth().then((result) => { + instagram.checkAuth(session).then((result) => { let view = result.isLoggedIn ? 'browser/index.html' : 'browser/login.html' session = result.session || session @@ -54,7 +54,7 @@ function createWindow () { } function getChatList () { - igdm.getChatList(session).then((chats) => { + instagram.getChatList(session).then((chats) => { mainWindow.webContents.send('chatList', chats) setTimeout(getChatList, pollingInterval); @@ -63,7 +63,7 @@ function getChatList () { let timeoutObj; function getChat (evt, id) { - igdm.getChat(session, id).then((chat) => { + instagram.getChat(session, id).then((chat) => { mainWindow.webContents.send('chat', chat); if (timeoutObj) clearTimeout(timeoutObj) @@ -103,14 +103,14 @@ app.on('browser-window-focus', () => { }) electron.ipcMain.on('login', (evt, data) => { - igdm.login(data.username, data.password).then((session_) => { + instagram.login(data.username, data.password).then((session_) => { session = session_ createWindow() }).catch(() => createWindow()) }) electron.ipcMain.on('logout', (evt, data) => { - igdm.logout() + instagram.logout() session = null createWindow() }) @@ -125,19 +125,19 @@ electron.ipcMain.on('getChatList', getChatList) electron.ipcMain.on('getChat', getChat) electron.ipcMain.on('message', (evt, data) => { - igdm.sendMessage(session, data.message, data.users).then((chat) => { + instagram.sendMessage(session, data.message, data.users).then((chat) => { if (data.isNewChat) getChat(null, chat[0].id) }) }) electron.ipcMain.on('searchUsers', (evt, search) => { - igdm.searchUsers(session, search).then((users) => { + instagram.searchUsers(session, search).then((users) => { mainWindow.webContents.send('searchResult', users); }) }) electron.ipcMain.on('markAsRead', (evt, thread) => { - igdm.seen(session, thread) + instagram.seen(session, thread) }) electron.ipcMain.on('notify', (evt, message) => { @@ -147,11 +147,11 @@ electron.ipcMain.on('notify', (evt, message) => { }) electron.ipcMain.on('getUnfollowers', (evt) => { - igdm.getUnfollowers(session).then((users) => { + instagram.getUnfollowers(session).then((users) => { mainWindow.webContents.send('unfollowers', users) }) }) electron.ipcMain.on('unfollow', (evt, userId) => { - igdm.unfollow(session, userId) + instagram.unfollow(session, userId) }) diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..6b29854 --- /dev/null +++ b/utils.js @@ -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 +}