Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Testing {
- public static void main(String[] args) {
- String filePath = "H:/result.txt"; // This is the file which will have the end-result
- new File(filePath).delete(); // Delete it so we start a new fresh operation
- String allLines = "";
- for (int row = 2; row <= 100; row++) { // levels in Duolingo homePage are presented in rows on top of each other
- System.out.println(row); // Just to monitor the progress
- for (int level = 1; level <= 3; level++) { // level represents a single level, e.g: #skill-2-5 > a. There can't be more than 3 levels in one row
- allLines += "\n" + "TAG SELECTOR=\"div._2GJb6:nth-of-type(" + row + ") > a:nth-child(" + level + ")\" EXTRACT=HREF"; // Get the level url
- allLines += "\n" + "SET !VAR0 EVAL(\"'{{!EXTRACT}}'=='#EANF#' ? 0 : 5;\")"; // If the url doesn't exist, don't waste time waiting for a no-page to load
- allLines += "\n" + "SET !VAR1 EVAL(\"'{{!EXTRACT}}'=='#EANF#' ? '' : 'https://www.duolingo.com'+'{{!EXTRACT}}';\")"; // If the relative url wasn't found, don't waste time opening duolingo.com home page
- allLines += "\n" + "TAB OPEN";
- allLines += "\n" + "TAB T=2";
- allLines += "\n" + "URL GOTO={{!VAR1}}"; // Open the level in new tab
- allLines += "\n" + "WAIT SECONDS={{!VAR0}}";
- allLines += "\n" + "SET !EXTRACT NULL"; // We don't need the link to end up in the CSV
- allLines += "\n" + "TAG SELECTOR=\"div.yZINH._1_vhy > h2\" EXTRACT=TXT"; // Get the level's title
- allLines += "\n" + "SET levelTitle {{!EXTRACT}}"; // We don't want to keep extracting the title everytime we need to add it to the CSV
- allLines += "\n" + "SET !EXTRACT NULL"; // Not now, we don't want it now
- for (int lesson_item = 1; lesson_item <= 10; lesson_item++) { // There can't be more than 10 lessons in one level
- allLines += "\n" + "TAG SELECTOR=\"div._1DWXz > div > div.kHldG:nth-of-type(" + lesson_item + ") > div > div > p\" EXTRACT=TXT";
- allLines += "\n" + "SET wordsList {{!EXTRACT}}"; // This is the comma-delimited list of words
- allLines += "\n" + "SET !EXTRACT NULL"; // We don't want the comma-delimited list of words to end up in the CSV
- for (int wordIndex = 0; wordIndex <= 9; wordIndex++) { // For every word in the wordsList (there can't more than 10)
- allLines += "\n" + "SET !VAR" + wordIndex + " EVAL(\"var words='{{wordsList}}'.split(','); words[" + wordIndex + "] == null ? '#EANF#' : words[" + wordIndex + "];\")";
- allLines += "\n" + "ADD !EXTRACT {{levelTitle}}";
- allLines += "\n" + "ADD !EXTRACT {{!VAR" + wordIndex + "}}"; // the !VAR will hold a single word from the wordsList
- allLines += "\n" + "SAVEAS TYPE=EXTRACT FOLDER=* FILE=H:\\Duolingo<SP>Home<SP>List.csv"; // Save to start new line
- }
- }
- allLines += "\n" + "TAB T=1";
- allLines += "\n" + "TAB CLOSEALLOTHERS";
- allLines += "\n";
- allLines += "\n";
- }
- saveToFile(filePath, true, allLines); // Save the buffer in the file
- allLines = ""; // Free the buffer to speed things up
- }
- }
- static void saveToFile(String filePath, boolean append, String linesToWrite) {
- try {
- FileWriter fw = new FileWriter(new File(filePath), append);
- fw.write(linesToWrite);
- fw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement