From d207fdda4559371c7b7dace16a771c23a9bdf954 Mon Sep 17 00:00:00 2001 From: marco Date: Sat, 25 Jul 2015 00:48:57 +0200 Subject: [PATCH] Fix login --- org/fbot/Fbot.java | 109 ++++++++++++++++++++++++--------------------- org/ft/Up.java | 2 +- 2 files changed, 59 insertions(+), 52 deletions(-) diff --git a/org/fbot/Fbot.java b/org/fbot/Fbot.java index d90be5a..ddb7317 100644 --- a/org/fbot/Fbot.java +++ b/org/fbot/Fbot.java @@ -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"; @@ -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 l = new ArrayList(); 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 f = new ArrayList(); 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(); } } @@ -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 { @@ -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); } } - diff --git a/org/ft/Up.java b/org/ft/Up.java index a74b82b..ceb54c5 100644 --- a/org/ft/Up.java +++ b/org/ft/Up.java @@ -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 fails = new ArrayList(); - 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;