Skip to content

Commit 2602a6c

Browse files
committed
Login with session in progress
1 parent f48522a commit 2602a6c

3 files changed

Lines changed: 89 additions & 4 deletions

File tree

app/src/main/java/com/moithepro/instatoolsandroid/LoginActivity.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import com.moithepro.instatoolsandroid.jInstaloader.JInvalidArgumentException;
3030
import com.moithepro.instatoolsandroid.jInstaloader.JTwoFactorAuthRequiredException;
3131

32+
import java.io.FileNotFoundException;
33+
3234
public class LoginActivity extends AppCompatActivity {
3335
private EditText password;
3436
private EditText username;
@@ -74,7 +76,11 @@ protected void onCreate(Bundle savedInstanceState) {
7476
String u = username.getText().toString().trim();
7577
String p = password.getText().toString().trim();
7678
loader.login(u, p);
77-
79+
try {
80+
loader.saveSession("session");
81+
} catch (Exception e) {
82+
e.printStackTrace();
83+
}
7884
mainHandler.post(() -> {
7985
sp.edit().putString(getString(R.string.username_key), u).putString(getString(R.string.password_key), p).apply();
8086

@@ -332,7 +338,40 @@ protected void onCreate(Bundle savedInstanceState) {
332338
Handler mainHandler = new Handler(Looper.getMainLooper());
333339
loginThread = new Thread(() -> {
334340

341+
try {
342+
loader.loadSession("session");
343+
mainHandler.post(() -> {
344+
Intent intent = new Intent(this, UsersActivity.class);
345+
startActivity(intent);
346+
loggingIn = false;
347+
password.setEnabled(true);
348+
username.setEnabled(true);
349+
login.setText(R.string.log_in);
350+
finish();
351+
//setContentView(R.layout.users_activity);
352+
353+
});
354+
}catch (FileNotFoundException e){
355+
mainHandler.post(() -> {
356+
AD = new AlertDialog.Builder(this)
357+
.setTitle("Error")
358+
.setMessage("Session File Not Found")
359+
360+
// Specifying a listener allows you to take an action before dismissing the dialog.
361+
// The dialog is automatically dismissed when a dialog button is clicked.
362+
.setPositiveButton(android.R.string.ok, null)
335363

364+
// A null listener allows the button to dismiss the dialog and take no further action.
365+
.show();
366+
loggingIn = false;
367+
password.setEnabled(true);
368+
username.setEnabled(true);
369+
login.setText(R.string.log_in);
370+
});
371+
}
372+
catch (Exception e){
373+
e.printStackTrace();
374+
}
336375
try {
337376
loader.login(spu, spp);
338377

app/src/main/java/com/moithepro/instatoolsandroid/jInstaloader/JInstaloader.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.chaquo.python.PyObject;
55
import com.chaquo.python.Python;
66

7+
import java.io.FileNotFoundException;
78
import java.io.Serializable;
89
import java.util.ArrayList;
910
import java.util.List;
@@ -18,6 +19,8 @@ public class JInstaloader {
1819
public static final int E_TwoFactorAuthRequiredException = 6;
1920
public static final int E_PrivateProfileNotFollowedException = 7;
2021
public static final int E_Exception = 8;
22+
23+
public static final int E_FileNotFoundError = 9;
2124
private Python py;
2225
private PyObject instaloaderInterface;
2326
private String loggedInUsername = null;
@@ -55,7 +58,30 @@ private void ignoreThisMethodItHasNoPracticalUsage(String username, String passw
5558
throw new JException();
5659
}
5760
}
58-
61+
public void loadSession(String filename) throws FileNotFoundException, JException {
62+
PyObject res = instaloaderInterface.callAttr("load_session", instaloaderInterface, filename);
63+
int err = res.callAttr("get_e").toInt();
64+
switch (err) {
65+
case NO_ERROR:
66+
break;
67+
case E_FileNotFoundError:
68+
throw new FileNotFoundException();
69+
case E_Exception:
70+
throw new JException();
71+
}
72+
}
73+
public void saveSession(String filename) throws JException, JLoginRequiredException {
74+
PyObject res = instaloaderInterface.callAttr("save_session", instaloaderInterface, filename);
75+
int err = res.callAttr("get_e").toInt();
76+
switch (err) {
77+
case NO_ERROR:
78+
break;
79+
case E_LoginRequiredException:
80+
throw new JLoginRequiredException();
81+
case E_Exception:
82+
throw new JException();
83+
}
84+
}
5985
public void login(String username, String password) throws JInvalidArgumentException, JConnectionException, JBadCredentialsException, JTwoFactorAuthRequiredException, JException {
6086

6187
PyObject res = instaloaderInterface.callAttr("login", instaloaderInterface, username, password);

app/src/main/python/interface.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
E_TwoFactorAuthRequiredException = 6
1515
E_PrivateProfileNotFollowedException = 7
1616
E_Exception = 8
17-
17+
E_FileNotFoundError = 9
1818

1919
class Result:
2020

@@ -32,7 +32,27 @@ def get_arg(self):
3232
class Interface:
3333
def __init__(self):
3434
self.L: Instaloader = None
35-
35+
def load_session(self, session):
36+
try:
37+
print("load")
38+
self.L = instaloader.Instaloader()
39+
self.L.load_session_from_file(session)
40+
return Result(NO_ERROR, 0)
41+
except FileNotFoundError as err:
42+
return Result(E_FileNotFoundError, 0)
43+
except Exception as ex:
44+
print(ex)
45+
return Result(E_Exception, 0)
46+
def save_session(self, session):
47+
try:
48+
print("save")
49+
self.L.save_session_to_file(session)
50+
return Result(NO_ERROR, 0)
51+
except LoginRequiredException as err:
52+
return Result(E_LoginRequiredException, 0)
53+
except Exception as ex:
54+
print(ex)
55+
return Result(E_Exception, 0)
3656
def login(self, username, password):
3757
try:
3858
print("login")

0 commit comments

Comments
 (0)