Skip to content

Commit

Permalink
Merge pull request #5 from MarcoFalke/fix-login
Browse files Browse the repository at this point in the history
Fix login issues; fixes #2
  • Loading branch information
MarcoFalke committed Jul 24, 2015
2 parents 307b5ab + d207fdd commit c13372d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 52 deletions.
109 changes: 58 additions & 51 deletions org/fbot/Fbot.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,54 +23,57 @@ public class Fbot {
private Fbot() {
}

public static void loginAndSetPrefs(Wiki wiki, String user, char[] p) {
public static boolean loginAndSetPrefs(Wiki wiki, String user, char[] p) {
wiki.setThrottle(1);
wiki.setMaxLag(-1);
int i = 0;
boolean success = false;
do {
while (++i < 8)
try {
wiki.login(user, p);
success = true;
continue;
}
catch (IOException e) {
System.out.println("Encountered IOException. This was try #" + i);
if (++i <= 8) continue;
System.exit(1);
continue;
}
catch (FailedLoginException e) {
return true;
} catch (IOException e) {
System.out.println("Encountered IOException. This was try #"
+ i);
} catch (FailedLoginException e) {
e.printStackTrace();
System.exit(1);
return false;
}
} while (!success);
return false;
}

public static void guiLogin(Wiki wiki) {
JTextField u = new JTextField(12);
JPasswordField px = new JPasswordField(12);
JPanel pl = FbotUtil.buildForm("Login", new JLabel("Username:", 11), u, new JLabel("Password:", 11), px);
if (JOptionPane.showConfirmDialog(null, pl, "Login", 2, -1) != 0) {
System.exit(0);
}
Fbot.loginAndSetPrefs(wiki, u.getText().trim(), px.getPassword());
do {

JPanel pl = FbotUtil.buildForm("Login",
new JLabel("Username:", 11), u,
new JLabel("Password:", 11), px);
if (JOptionPane.showConfirmDialog(null, pl, "Login", 2, -1) != 0) {
System.exit(0);
}
} while (!Fbot.loginAndSetPrefs(wiki, u.getText().trim(),
px.getPassword()));

wiki.setThrottle(5);
}

public static String getRedirectTarget(String redirect, Wiki wiki) throws IOException {
public static String getRedirectTarget(String redirect, Wiki wiki)
throws IOException {
String text = wiki.getPageText(redirect).trim();
if (text.matches("(?si)^#(redirect)\\s*?\\[\\[.+?\\]\\].*?")) {
return text.substring(text.indexOf("[[") + 2, text.indexOf("]]"));
}
throw new UnsupportedOperationException("Parameter passed in is not a redirect page!");
throw new UnsupportedOperationException(
"Parameter passed in is not a redirect page!");
}

public static boolean exists(String page, Wiki wiki) throws IOException {
return (Boolean)wiki.getPageInfo(page).get("exists");
return (Boolean) wiki.getPageInfo(page).get("exists");
}

public static void dbrDump(String page, String[] list, String headerText, String footerText, Wiki wiki) throws LoginException, IOException {
public static void dbrDump(String page, String[] list, String headerText,
String footerText, Wiki wiki) throws LoginException, IOException {
String dump = headerText + " This report last updated as of ~~~~~\n";
for (String s : list) {
dump = dump + "*[[:" + s + "]]\n";
Expand All @@ -79,58 +82,63 @@ public static void dbrDump(String page, String[] list, String headerText, String
wiki.edit(page, dump, "Updating list");
}

public static String[] listNamespaceSort(String[] list, int namespace, Wiki wiki) throws IOException {
public static String[] listNamespaceSort(String[] list, int namespace,
Wiki wiki) throws IOException {
ArrayList<String> l = new ArrayList<String>();
for (String s : list) {
if (wiki.namespace(s) != namespace) continue;
if (wiki.namespace(s) != namespace)
continue;
l.add(s);
}
return l.toArray(new String[0]);
}

public static String[] arrayNuke(String[] list, String reason, String talkReason, Wiki wiki) {
public static String[] arrayNuke(String[] list, String reason,
String talkReason, Wiki wiki) {
ArrayList<String> f = new ArrayList<String>();
for (String s : list) {
try {
wiki.delete(s, reason);
}
catch (Throwable e) {
} catch (Throwable e) {
f.add(s);
continue;
}
if (talkReason == null) continue;
if (talkReason == null)
continue;
try {
wiki.delete(wiki.getTalkPage(s), talkReason);
continue;
}
catch (Throwable e) {
} catch (Throwable e) {
// empty catch block
}
}
return f.toArray(new String[0]);
}

public static void addTextList(String[] pages, String text, String summary, Wiki wiki) {
public static void addTextList(String[] pages, String text, String summary,
Wiki wiki) {
for (String page : pages) {
try {
wiki.edit(page, wiki.getPageText(text) + text, summary);
continue;
}
catch (Throwable e) {
} catch (Throwable e) {
e.printStackTrace();
}
}
}

public static void fileReplace(String[] list, String file, String replacement, String summary, Wiki wiki) {
file = file.replace((CharSequence)"_", (CharSequence)" ");
String regex = "(?i)(" + file + "|" + file.replace((CharSequence)" ", (CharSequence)"_") + ")";
public static void fileReplace(String[] list, String file,
String replacement, String summary, Wiki wiki) {
file = file.replace((CharSequence) "_", (CharSequence) " ");
String regex = "(?i)(" + file + "|"
+ file.replace((CharSequence) " ", (CharSequence) "_") + ")";
for (String page : list) {
try {
wiki.edit(page, wiki.getPageText(page).replaceAll(regex, replacement), summary);
wiki.edit(page,
wiki.getPageText(page).replaceAll(regex, replacement),
summary);
continue;
}
catch (Throwable e) {
} catch (Throwable e) {
e.printStackTrace();
}
}
Expand All @@ -142,13 +150,15 @@ public static Wiki wikiFactory(String u, char[] p, String domain) {
return wiki;
}

public static void downloadFile(String title, String localpath, Wiki wiki) throws IOException, FileNotFoundException {
public static void downloadFile(String title, String localpath, Wiki wiki)
throws IOException, FileNotFoundException {
FileOutputStream fos = new FileOutputStream(localpath);
fos.write(wiki.getImage(title));
fos.close();
}

public static void superAction(Wiki wiki, String page, String reason, String code) throws LoginException, IOException {
public static void superAction(Wiki wiki, String page, String reason,
String code) throws LoginException, IOException {
boolean success = false;
int i = 0;
do {
Expand All @@ -158,26 +168,23 @@ public static void superAction(Wiki wiki, String page, String reason, String cod
} else if (code.equals("upload")) {
wiki.upload(new File(page), page, reason, "");
} else {
throw new UnsupportedOperationException(code + " is not a valid code!");
throw new UnsupportedOperationException(code
+ " is not a valid code!");
}
success = true;
continue;
}
catch (LoginException e) {
} catch (LoginException e) {
throw e;
}
catch (IOException e) {
} catch (IOException e) {
if (i++ > 4) {
throw e;
}
e.printStackTrace();
System.err.println("Network error? Try: " + i + " of 5");
continue;
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
} while (!success);
}
}

2 changes: 1 addition & 1 deletion org/ft/Up.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class Up {
private static final Wiki wiki = new Wiki("commons.wikimedia.org");
private static final String text = "== {{int:filedesc}} ==\n{{Information\n|Description=%s\n|Source=%s\n|Date=%s\n|Author=%s\n|Permission=\n|other_versions=\n}}\n\n== {{int:license-header}} ==\n%s\n%s";
private static final ArrayList<String> fails = new ArrayList<String>();
private static final String VERSION = "v0.2.1";
private static final String VERSION = "v0.2.2";
private static final String NAME = "Up! " + VERSION;
private static final JFrame f = new JFrame(NAME +" - Wikimedia Commons Mass Uploader");
private static int cnt = 0;
Expand Down

0 comments on commit c13372d

Please sign in to comment.