Skip to content

Commit 60c1575

Browse files
committed
continue login session fix
1 parent 5eb744f commit 60c1575

3 files changed

Lines changed: 148 additions & 121 deletions

File tree

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

Lines changed: 127 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import android.view.View;
1919
import android.widget.EditText;
2020
import android.widget.TextView;
21+
import android.widget.Toast;
2122

2223
import com.chaquo.python.Python;
2324
import com.chaquo.python.android.AndroidPlatform;
@@ -29,6 +30,7 @@
2930
import com.moithepro.instatoolsandroid.jInstaloader.JInvalidArgumentException;
3031
import com.moithepro.instatoolsandroid.jInstaloader.JTwoFactorAuthRequiredException;
3132

33+
import java.io.File;
3234
import java.io.FileNotFoundException;
3335

3436
public class LoginActivity extends AppCompatActivity {
@@ -76,8 +78,12 @@ protected void onCreate(Bundle savedInstanceState) {
7678
String u = username.getText().toString().trim();
7779
String p = password.getText().toString().trim();
7880
loader.login(u, p);
81+
File file = new File(this.getFilesDir(), "session");
7982
try {
83+
84+
//loader.saveSession(file.getCanonicalPath());
8085
loader.saveSession("session");
86+
8187
} catch (Exception e) {
8288
e.printStackTrace();
8389
}
@@ -337,21 +343,24 @@ protected void onCreate(Bundle savedInstanceState) {
337343

338344
Handler mainHandler = new Handler(Looper.getMainLooper());
339345
loginThread = new Thread(() -> {
340-
346+
File file = new File(this.getFilesDir(), "session");
341347
try {
342-
loader.loadSession("session");
348+
349+
//loader.loadSession(file.getCanonicalPath());
350+
loader.loadSession(spu,"session");
343351
mainHandler.post(() -> {
344352
Intent intent = new Intent(this, UsersActivity.class);
345353
startActivity(intent);
346354
loggingIn = false;
347355
password.setEnabled(true);
348356
username.setEnabled(true);
357+
Toast.makeText(this, "Logged in as @" + loader.getLoggedInUsername(), Toast.LENGTH_SHORT).show();
349358
login.setText(R.string.log_in);
350359
finish();
351360
//setContentView(R.layout.users_activity);
352361

353362
});
354-
}catch (FileNotFoundException e){
363+
} catch (FileNotFoundException er) {
355364
mainHandler.post(() -> {
356365
AD = new AlertDialog.Builder(this)
357366
.setTitle("Error")
@@ -363,116 +372,14 @@ protected void onCreate(Bundle savedInstanceState) {
363372

364373
// A null listener allows the button to dismiss the dialog and take no further action.
365374
.show();
366-
loggingIn = false;
367-
password.setEnabled(true);
368-
username.setEnabled(true);
369-
login.setText(R.string.log_in);
375+
370376
});
371-
}
372-
catch (Exception e){
377+
login(spu, spp);
378+
} catch (Exception e) {
373379
e.printStackTrace();
380+
login(spu, spp);
374381
}
375-
try {
376-
loader.login(spu, spp);
377-
378-
mainHandler.post(() -> {
379-
Intent intent = new Intent(this, UsersActivity.class);
380-
startActivity(intent);
381-
loggingIn = false;
382-
password.setEnabled(true);
383-
username.setEnabled(true);
384-
login.setText(R.string.log_in);
385-
finish();
386-
//setContentView(R.layout.users_activity);
387-
388-
});
389-
} catch (JInvalidArgumentException e) {
390-
mainHandler.post(() -> {
391-
AD = new AlertDialog.Builder(this)
392-
.setTitle("Error")
393-
.setMessage("Username Does not Exist")
394-
395-
// Specifying a listener allows you to take an action before dismissing the dialog.
396-
// The dialog is automatically dismissed when a dialog button is clicked.
397-
.setPositiveButton(android.R.string.ok, null)
398-
399-
// A null listener allows the button to dismiss the dialog and take no further action.
400-
.show();
401-
loggingIn = false;
402-
password.setEnabled(true);
403-
username.setEnabled(true);
404-
login.setText(R.string.log_in);
405-
});
406-
} catch (JConnectionException e) {
407-
mainHandler.post(() -> {
408-
AD = new AlertDialog.Builder(this)
409-
.setTitle("Error")
410-
.setMessage("Connection Failed")
411-
412-
// Specifying a listener allows you to take an action before dismissing the dialog.
413-
// The dialog is automatically dismissed when a dialog button is clicked.
414-
.setPositiveButton(android.R.string.ok, null)
415-
416-
// A null listener allows the button to dismiss the dialog and take no further action.
417-
.show();
418-
loggingIn = false;
419-
password.setEnabled(true);
420-
username.setEnabled(true);
421-
login.setText(R.string.log_in);
422-
});
423382

424-
} catch (JBadCredentialsException e) {
425-
mainHandler.post(() -> {
426-
AD = new AlertDialog.Builder(this)
427-
.setTitle("Error")
428-
.setMessage("Password is Wrong")
429-
430-
// Specifying a listener allows you to take an action before dismissing the dialog.
431-
// The dialog is automatically dismissed when a dialog button is clicked.
432-
.setPositiveButton(android.R.string.ok, null)
433-
434-
// A null listener allows the button to dismiss the dialog and take no further action.
435-
.show();
436-
loggingIn = false;
437-
password.setEnabled(true);
438-
username.setEnabled(true);
439-
login.setText(R.string.log_in);
440-
});
441-
} catch (JTwoFactorAuthRequiredException e) {
442-
mainHandler.post(() -> {
443-
AD = new AlertDialog.Builder(this)
444-
.setTitle("Error")
445-
.setMessage("Two Factor Authentication is required but is not supported right now. you can try logging in with another account.")
446-
447-
// Specifying a listener allows you to take an action before dismissing the dialog.
448-
// The dialog is automatically dismissed when a dialog button is clicked.
449-
.setPositiveButton(android.R.string.ok, null)
450-
451-
// A null listener allows the button to dismiss the dialog and take no further action.
452-
.show();
453-
loggingIn = false;
454-
password.setEnabled(true);
455-
username.setEnabled(true);
456-
login.setText(R.string.log_in);
457-
});
458-
} catch (JException e) {
459-
mainHandler.post(() -> {
460-
AD = new AlertDialog.Builder(this)
461-
.setTitle("Error")
462-
.setMessage("Unknown Error")
463-
464-
// Specifying a listener allows you to take an action before dismissing the dialog.
465-
// The dialog is automatically dismissed when a dialog button is clicked.
466-
.setPositiveButton(android.R.string.ok, null)
467-
468-
// A null listener allows the button to dismiss the dialog and take no further action.
469-
.show();
470-
loggingIn = false;
471-
password.setEnabled(true);
472-
username.setEnabled(true);
473-
login.setText(R.string.log_in);
474-
});
475-
}
476383
});
477384
loginThread.start();
478385
}
@@ -498,7 +405,118 @@ protected void onCreate(Bundle savedInstanceState) {
498405
});
499406

500407
}
408+
private void login(String u, String p) {
409+
Handler mainHandler = new Handler(Looper.getMainLooper());
410+
try {
411+
loader.login(u, p);
412+
413+
try {
414+
//loader.saveSession(file.getCanonicalPath());
415+
loader.saveSession("session");
416+
} catch (Exception e) {
417+
e.printStackTrace();
418+
}
501419

420+
421+
mainHandler.post(() -> {
422+
Intent intent = new Intent(this, UsersActivity.class);
423+
startActivity(intent);
424+
loggingIn = false;
425+
password.setEnabled(true);
426+
username.setEnabled(true);
427+
login.setText(R.string.log_in);
428+
finish();
429+
//setContentView(R.layout.users_activity);
430+
431+
});
432+
} catch (JInvalidArgumentException e) {
433+
mainHandler.post(() -> {
434+
AD = new AlertDialog.Builder(this)
435+
.setTitle("Error")
436+
.setMessage("Username Does not Exist")
437+
438+
// Specifying a listener allows you to take an action before dismissing the dialog.
439+
// The dialog is automatically dismissed when a dialog button is clicked.
440+
.setPositiveButton(android.R.string.ok, null)
441+
442+
// A null listener allows the button to dismiss the dialog and take no further action.
443+
.show();
444+
loggingIn = false;
445+
password.setEnabled(true);
446+
username.setEnabled(true);
447+
login.setText(R.string.log_in);
448+
});
449+
} catch (JConnectionException e) {
450+
mainHandler.post(() -> {
451+
AD = new AlertDialog.Builder(this)
452+
.setTitle("Error")
453+
.setMessage("Connection Failed")
454+
455+
// Specifying a listener allows you to take an action before dismissing the dialog.
456+
// The dialog is automatically dismissed when a dialog button is clicked.
457+
.setPositiveButton(android.R.string.ok, null)
458+
459+
// A null listener allows the button to dismiss the dialog and take no further action.
460+
.show();
461+
loggingIn = false;
462+
password.setEnabled(true);
463+
username.setEnabled(true);
464+
login.setText(R.string.log_in);
465+
});
466+
467+
} catch (JBadCredentialsException e) {
468+
mainHandler.post(() -> {
469+
AD = new AlertDialog.Builder(this)
470+
.setTitle("Error")
471+
.setMessage("Password is Wrong")
472+
473+
// Specifying a listener allows you to take an action before dismissing the dialog.
474+
// The dialog is automatically dismissed when a dialog button is clicked.
475+
.setPositiveButton(android.R.string.ok, null)
476+
477+
// A null listener allows the button to dismiss the dialog and take no further action.
478+
.show();
479+
loggingIn = false;
480+
password.setEnabled(true);
481+
username.setEnabled(true);
482+
login.setText(R.string.log_in);
483+
});
484+
} catch (JTwoFactorAuthRequiredException e) {
485+
mainHandler.post(() -> {
486+
AD = new AlertDialog.Builder(this)
487+
.setTitle("Error")
488+
.setMessage("Two Factor Authentication is required but is not supported right now. you can try logging in with another account.")
489+
490+
// Specifying a listener allows you to take an action before dismissing the dialog.
491+
// The dialog is automatically dismissed when a dialog button is clicked.
492+
.setPositiveButton(android.R.string.ok, null)
493+
494+
// A null listener allows the button to dismiss the dialog and take no further action.
495+
.show();
496+
loggingIn = false;
497+
password.setEnabled(true);
498+
username.setEnabled(true);
499+
login.setText(R.string.log_in);
500+
});
501+
} catch (JException e) {
502+
mainHandler.post(() -> {
503+
AD = new AlertDialog.Builder(this)
504+
.setTitle("Error")
505+
.setMessage("Unknown Error")
506+
507+
// Specifying a listener allows you to take an action before dismissing the dialog.
508+
// The dialog is automatically dismissed when a dialog button is clicked.
509+
.setPositiveButton(android.R.string.ok, null)
510+
511+
// A null listener allows the button to dismiss the dialog and take no further action.
512+
.show();
513+
loggingIn = false;
514+
password.setEnabled(true);
515+
username.setEnabled(true);
516+
login.setText(R.string.log_in);
517+
});
518+
}
519+
}
502520
@Override
503521
protected void onDestroy() {
504522
super.onDestroy();

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ private void ignoreThisMethodItHasNoPracticalUsage(String username, String passw
5858
throw new JException();
5959
}
6060
}
61-
public void loadSession(String filename) throws FileNotFoundException, JException {
62-
PyObject res = instaloaderInterface.callAttr("load_session", instaloaderInterface, filename);
61+
public void loadSession(String username,String filename) throws FileNotFoundException, JException {
62+
PyObject res = instaloaderInterface.callAttr("load_session", instaloaderInterface,username, filename);
6363
int err = res.callAttr("get_e").toInt();
6464
switch (err) {
6565
case NO_ERROR:
66+
loggedInUsername = res.callAttr("get_arg").toString();
6667
break;
6768
case E_FileNotFoundError:
6869
throw new FileNotFoundException();

app/src/main/python/interface.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import instaloader
2+
import os
3+
from os.path import join, dirname
4+
from com.chaquo.python import Python
25
from instaloader import ConnectionException, InvalidArgumentException, BadCredentialsException, \
36
ProfileNotExistsException, LoginRequiredException, Instaloader, Profile, \
47
TwoFactorAuthRequiredException, \
@@ -32,27 +35,32 @@ def get_arg(self):
3235
class Interface:
3336
def __init__(self):
3437
self.L: Instaloader = None
35-
def load_session(self, session):
38+
def load_session(self,username, session):
3639
try:
3740
print("load")
3841
self.L = instaloader.Instaloader()
39-
self.L.load_session_from_file(session)
40-
return Result(NO_ERROR, 0)
42+
#files_dir = str(Python.getPlatform().getApplication().getFilesDir())
43+
#self.L.load_session_from_file(join(dirname(__file__),session))
44+
self.L.load_session_from_file(username,filename=join(os.environ["HOME"],session))
45+
return Result(NO_ERROR, Profile.own_profile(self.L.context).username)
4146
except FileNotFoundError as err:
47+
print(err)
4248
return Result(E_FileNotFoundError, 0)
43-
except Exception as ex:
44-
print(ex)
45-
return Result(E_Exception, 0)
49+
#except Exception as ex:
50+
# print(ex)
51+
# return Result(E_Exception, 0)
4652
def save_session(self, session):
4753
try:
4854
print("save")
49-
self.L.save_session_to_file(session)
55+
#files_dir = str(Python.getPlatform().getApplication().getFilesDir())
56+
self.L.save_session_to_file(join(os.environ["HOME"],session))
5057
return Result(NO_ERROR, 0)
5158
except LoginRequiredException as err:
59+
print(err)
5260
return Result(E_LoginRequiredException, 0)
53-
except Exception as ex:
54-
print(ex)
55-
return Result(E_Exception, 0)
61+
#except Exception as ex:
62+
# print(ex)
63+
# return Result(E_Exception, 0)
5664
def login(self, username, password):
5765
try:
5866
print("login")

0 commit comments

Comments
 (0)