Title: [Java] [Java] GTDownloader_v0.1 Author: Anonymous Pastebin link: http://pastebin.com/AFLbeJpm First Edit: Monday 13th of April 2015 03:50:39 PM CDT Last Edit: Monday 13th of April 2015 03:50:39 PM CDT import java.awt.Color; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.net.MalformedURLException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.util.ArrayList; import java.util.List; import java.util.Scanner;   import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField;   public class GTDownloader implements ActionListener, KeyListener {           BufferedReader input;           File inputFile;         File unsucFile;         String[] arr;         String[] unsucArr;         List unsuc;           String s = null;         String writefag = new String();         String title = new String();         String link = new String();         String filepath = new String();         boolean problems;         int requestCount = 0;         URL pastebin = null;           JFrame frame;         JPanel panel;         JTextField sepText;         JRadioButton modeB1;         JRadioButton modeB2;         JTextField inputText;         JTextField outputText;         JCheckBox metadata;         JCheckBox subfolder;         JTextField strucText;         JButton[] strB = new JButton[3];         JButton startD;         JTextField statusText;           String seperator;         String sep1 = "";         String sep2 = "";         boolean b1;         boolean b2;         boolean b3;         int errorID;           public GTDownloader() {                   panel = new JPanel();                 panel.setLayout(null);                 // panel.setBackground(Color.RED);                 panel.setPreferredSize(new Dimension(400, 120));                   JLabel modeL = new JLabel("Parsing mode:");                 modeL.setBounds(0, 0, 85, 20);                 panel.add(modeL);                   modeB1 = new JRadioButton("Links only");                 modeB1.setBounds(85, 0, 90, 20);                 modeB1.addActionListener(this);                 panel.add(modeB1);                   modeB2 = new JRadioButton("Formatted");                 modeB2.setSelected(true);                 modeB2.setBounds(175, 0, 90, 20);                 modeB2.addActionListener(this);                 panel.add(modeB2);                   ButtonGroup group = new ButtonGroup();                 group.add(modeB1);                 group.add(modeB2);                   metadata = new JCheckBox("Save metadata");                 metadata.setSelected(true);                 metadata.setBounds(265, 0, 135, 20);                 metadata.addActionListener(this);                 panel.add(metadata);                   JLabel inputL = new JLabel("Input file:");                 inputL.setBounds(0, 20, 65, 20);                 panel.add(inputL);                   inputText = new JTextField("input.txt");                 inputText.setBounds(65, 20, 135, 20);                 panel.add(inputText);                   JLabel outputL = new JLabel("Output Folder:");                 outputL.setBounds(205, 20, 85, 20);                 panel.add(outputL);                   outputText = new JTextField("stories");                 outputText.setBounds(290, 20, 110, 20);                 panel.add(outputText);                   JLabel sepL = new JLabel("Seperator:");                 sepL.setBounds(0, 40, 65, 20);                 panel.add(sepL);                   sepText = new JTextField(">>><>>");                 sepText.setBounds(65, 40, 135, 20);                 sepText.addKeyListener(this);                 panel.add(sepText);                   subfolder = new JCheckBox("Create writefag subfolders", true);                 subfolder.setBounds(200, 40, 200, 20);                 subfolder.addActionListener(this);                 panel.add(subfolder);                   JLabel structureL = new JLabel("Input Syntax:");                 structureL.setBounds(0, 60, 80, 20);                 panel.add(structureL);                   strB[0] = new JButton("Writefag");                 strB[0].setBounds(85, 60, 105, 20);                 strB[0].addActionListener(this);                 panel.add(strB[0]);                   strB[1] = new JButton("Title");                 strB[1].setBounds(190, 60, 105, 20);                 strB[1].addActionListener(this);                 panel.add(strB[1]);                   strB[2] = new JButton("Link");                 strB[2].setBounds(295, 60, 105, 20);                 strB[2].addActionListener(this);                 panel.add(strB[2]);                   strucText = new JTextField(strB[0].getText() + sepText.getText()                                 + strB[1].getText() + sepText.getText() + strB[2].getText());                 strucText.setBounds(0, 80, 400, 20);                 strucText.setEditable(false);                 panel.add(strucText);                   startD = new JButton("Start download");                 startD.setBounds(0, 100, 120, 20);                 startD.addActionListener(this);                 panel.add(startD);                   statusText = new JTextField();                 statusText.setBounds(120, 100, 280, 20);                 statusText.setEditable(false);                 panel.add(statusText);                   frame = new JFrame();                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                 frame.setContentPane(panel);                 frame.pack();                 frame.setVisible(true);                   File outputFile;                   unsucFile = new File("unsuccessful.txt");                   try {                         pastebin = new URL("http://pastebin.com/");                 } catch (MalformedURLException e2) {                         // TODO Auto-generated catch block                         e2.printStackTrace();                 }                   unsuc = new ArrayList();           }           public void download(String s) { //              System.out.println(s);                 problems = false;                 writefag = "";                 title = "";                 link = "";                 String[] sa = processInput(s);                 for (int i = 0; i < strB.length; i++) {                         if (strB[i].getText().equals("Writefag")) {                                 writefag = sa[i];                         }                         if (strB[i].getText().equals("Title")) {                                 title = sa[i];                         }                         if (strB[i].getText().equals("Link")) {                                 link = sa[i];                         }                 }                   if (writefag.length() == 0) {                         writefag = "Anonymous";                 }                   try {                         if (title.length() == 0) {                                 title = link.substring(20);                         }                 } catch (Exception e) {                         title = "titleerror" + errorID;                 }                 if (link.length() == 0) {                         System.out.println("No link found.");                         problems = true;                 }                   try {                         link = link.substring(0, 20) + "raw.php?i=" + link.substring(20);                 } catch (Exception e) {                         problems = true;                 }                 // System.out.println(writefag + ":");                 // System.out                 // .println("Request No." + i + ": " + title + " at " + link);                 // System.out.println();                   // System.out.println("t: "+title);                 // System.out.println(link);                   filepath = outputText.getText() + "/" + rework(writefag) + "/"                                 + rework(title) + ".txt";                 statusText.setText(filepath);                 System.out.println(filepath);                 File pasteFile = new File(filepath);                   File path = new File(outputText.getText() + "/" + writefag);                 try {                         if (path.mkdirs()) {                                 System.out.println("Directory Created: " + outputText.getText()                                                 + "/" + writefag);                         }                 } catch (Exception e) {                         e.printStackTrace();                 }                   URL website = null;                 FileOutputStream fos = null;                 ReadableByteChannel rbc = null;                   if (!problems) {                         try {                                 website = new URL(link);                         } catch (MalformedURLException e) {                                 // TODO Auto-generated catch block                                 problems = true;                                 e.printStackTrace();                         }                 }                 if (!problems) {                         try {                                 rbc = Channels.newChannel(website.openStream());                         } catch (IOException e) {                                 // TODO Auto-generated catch block                                 try {                                         Channels.newChannel(pastebin.openStream());                                 } catch (IOException e1) {                                         // TODO Auto-generated catch block                                         System.out.println("Could not get access to pastebin.com.");                                         System.out                                                         .println("Either your internet connection broke down or paastebin.com temporarily blocked your ip due to \"unnatural browsing behaviour\".");                                         System.out                                                         .println("In case of the latter, reset your ip address or just wait until you're no longer blocked to continue.");                                         System.exit(0);                                   }                                 problems = true;                                 System.out.println("Could not open " + link);                         }                 }                 if (!problems) {                         try {                                 fos = new FileOutputStream(pasteFile);                         } catch (FileNotFoundException e) {                                 // TODO Auto-generated catch block                                 problems = true;                                 e.printStackTrace();                         }                 }                 if (!problems) {                         try {                                 fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);                         } catch (IOException e) {                                 // TODO Auto-generated catch block                                 problems = true;                                 e.printStackTrace();                         }                 }                 if (!problems) {                         try {                                 fos.close();                         } catch (IOException e) {                                 // TODO Auto-generated catch block                                 problems = true;                                 e.printStackTrace();                         }                 }           }           public String rework(String s) {                 s = s.replace("/", "_");                 s = s.replace("\\", "_");                 s = s.replace("*", "_");                 s = s.replace(":", "_");                 s = s.replace("?", "_");                 s = s.replace("\"", "'");                 s = s.replace(">", "_");                 s = s.replace("<", "_");                 s = s.replace("|", "_");                 return s;         }           public void saveList(int x) {                 try {                         PrintWriter saveWriter = new PrintWriter(inputFile);                         for (int i = x + 1; i < arr.length; i++) {                                 saveWriter.println(arr[i]);                         }                         saveWriter.close();                           unsucArr = unsuc.toArray(new String[0]);                         PrintWriter unsucWriter = new PrintWriter(unsucFile);                         for (int i = 0; i < arr.length; i++) {                                 unsucWriter.println(arr[i]);                         }                         unsucWriter.close();                 } catch (IOException e) {                         // TODO Auto-generated catch block                         e.printStackTrace();                 }         }           public String[] processInput(String input) {                 List sal = new ArrayList();                 while (input.indexOf(seperator) != -1) {                         sal.add(input.substring(0, input.indexOf(seperator)));                         input = input.substring(input.indexOf(seperator)                                         + seperator.length());                 }                 sal.add(input);                   return sal.toArray(new String[0]);         }           public static void main(String[] args) {                 new GTDownloader();         }           @Override         public void actionPerformed(ActionEvent e) {                   sepText.setEnabled(modeB2.isSelected());                 strB[0].setEnabled(modeB2.isSelected());                 strB[1].setEnabled(modeB2.isSelected());                 strB[2].setEnabled(modeB2.isSelected());                 strucText.setEnabled(modeB2.isSelected());                   if (!e.getSource().equals(modeB2) && !e.getSource().equals(modeB1)) {                         if (((JButton) e.getSource()).getText().equals("Writefag")) {                                 ((JButton) e.getSource()).setText("Title");                         } else if (((JButton) e.getSource()).getText().equals("Title")) {                                 ((JButton) e.getSource()).setText("Link");                         } else if (((JButton) e.getSource()).getText().equals("Link")) {                                 ((JButton) e.getSource()).setText("");                         } else if (((JButton) e.getSource()).getText().equals("")) {                                 ((JButton) e.getSource()).setText("Writefag");                         }                 }                   if (e.getSource().equals(strB[0]) || e.getSource().equals(strB[1])                                 || e.getSource().equals(strB[2])) {                         if (strB[0].getText().length() == 0) {                                 b1 = false;                         } else {                                 b1 = true;                         }                         if (strB[1].getText().length() == 0) {                                 b2 = false;                         } else {                                 b2 = true;                         }                         if (strB[2].getText().length() == 0) {                                 b3 = false;                         } else {                                 b3 = true;                         }                         if (b1 && b2 && b3) {                                 sep1 = sepText.getText();                                 sep2 = sepText.getText();                         }                         if (b1 && b2 && !b3) {                                 sep1 = sepText.getText();                                 sep2 = "";                         }                         if (b1 && !b2 && b3) {                                 sep1 = sepText.getText();                                 sep2 = "";                         }                         if (!b1 && b2 && b3) {                                 sep1 = "";                                 sep2 = sepText.getText();                         }                         if (!b1 && !b2 && b3) {                                 sep1 = "";                                 sep2 = "";                         }                         if (!b1 && b2 && !b3) {                                 sep1 = "";                                 sep2 = "";                         }                         if (b1 && !b2 && !b3) {                                 sep1 = "";                                 sep2 = "";                         }                         if (!b1 && !b2 && !b3) {                                 sep1 = "";                                 sep2 = "";                         }                         strucText.setText(strB[0].getText() + sep1 + strB[1].getText()                                         + sep2 + strB[2].getText());                 }                   if (e.getSource().equals(startD)) {                         boolean problems = false;                           inputFile = new File(inputText.getText());                         if (!problems) {                                 try {                                         input = new BufferedReader(new FileReader(inputFile));                                 } catch (IOException ex) {                                         // TODO Auto-generated catch block                                         statusText.setText("Could not find " + inputText.getText());                                         problems=true;                                 }                         }                         Scanner sc = null;                         if (!problems) {                                   try {                                         sc = new Scanner(inputFile);                                 } catch (FileNotFoundException e1) {                                         // TODO Auto-generated catch block                                         e1.printStackTrace();                                 }                         }                         if (!problems) {                                 List lines = new ArrayList();                                 while (sc.hasNextLine()) {                                         lines.add(sc.nextLine());                                 }                                 arr = lines.toArray(new String[0]);                                 System.out.println(arr.length);                                   seperator = sepText.getText();                                 for (int i = 0; i < arr.length; i++) {                                         // if (arr[i].length() != 0) {                                         download(arr[i]);                                         // }                                 }                         }                 }           }           @Override         public void keyTyped(KeyEvent e) {                 // TODO Auto-generated method stub                 strucText.setText(strB[0].getText() + sepText.getText()                                 + strB[1].getText() + sepText.getText() + strB[2].getText());           }           @Override         public void keyPressed(KeyEvent e) {                 // TODO Auto-generated method stub           }           @Override         public void keyReleased(KeyEvent e) {                 // TODO Auto-generated method stub                 strucText.setText(strB[0].getText() + sepText.getText()                                 + strB[1].getText() + sepText.getText() + strB[2].getText());         }   }