Advertisement
TheDemystifier

Duolingo Homes List

Jun 8th, 2017
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.81 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4.  
  5. public class Testing {
  6.     public static void main(String[] args) {
  7.         String filePath = "H:/result.txt"; // This is the file which will have the end-result
  8.         new File(filePath).delete(); // Delete it so we start a new fresh operation
  9.         String allLines = "";
  10.         for (int row = 2; row <= 100; row++) { // levels in Duolingo homePage are presented in rows on top of each other
  11.             System.out.println(row); // Just to monitor the progress
  12.             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
  13.                 allLines += "\n" + "TAG SELECTOR=\"div._2GJb6:nth-of-type(" + row + ") > a:nth-child(" + level + ")\" EXTRACT=HREF"; // Get the level url
  14.                 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
  15.                 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
  16.                 allLines += "\n" + "TAB OPEN";
  17.                 allLines += "\n" + "TAB T=2";
  18.                 allLines += "\n" + "URL GOTO={{!VAR1}}"; // Open the level in new tab
  19.                 allLines += "\n" + "WAIT SECONDS={{!VAR0}}";
  20.                 allLines += "\n" + "SET !EXTRACT NULL"; // We don't need the link to end up in the CSV
  21.                 allLines += "\n" + "TAG SELECTOR=\"div.yZINH._1_vhy > h2\" EXTRACT=TXT"; // Get the level's title
  22.                 allLines += "\n" + "SET levelTitle {{!EXTRACT}}"; // We don't want to keep extracting the title everytime we need to add it to the CSV
  23.                 allLines += "\n" + "SET !EXTRACT NULL"; // Not now, we don't want it now
  24.                 for (int lesson_item = 1; lesson_item <= 10; lesson_item++) { // There can't be more than 10 lessons in one level
  25.                     allLines += "\n" + "TAG SELECTOR=\"div._1DWXz > div > div.kHldG:nth-of-type(" + lesson_item + ") > div > div > p\" EXTRACT=TXT";
  26.                     allLines += "\n" + "SET wordsList {{!EXTRACT}}"; // This is the comma-delimited list of words
  27.                     allLines += "\n" + "SET !EXTRACT NULL"; // We don't want the comma-delimited list of words to end up in the CSV
  28.                     for (int wordIndex = 0; wordIndex <= 9; wordIndex++) { // For every word in the wordsList (there can't more than 10)
  29.                         allLines += "\n" + "SET !VAR" + wordIndex + " EVAL(\"var words='{{wordsList}}'.split(','); words[" + wordIndex + "] == null ? '#EANF#' : words[" + wordIndex + "];\")";
  30.                         allLines += "\n" + "ADD !EXTRACT {{levelTitle}}";
  31.                         allLines += "\n" + "ADD !EXTRACT {{!VAR" + wordIndex + "}}"; // the !VAR will hold a single word from the wordsList
  32.                         allLines += "\n" + "SAVEAS TYPE=EXTRACT FOLDER=* FILE=H:\\Duolingo<SP>Home<SP>List.csv"; // Save to start new line
  33.                     }
  34.                 }
  35.                 allLines += "\n" + "TAB T=1";
  36.                 allLines += "\n" + "TAB CLOSEALLOTHERS";
  37.                 allLines += "\n";
  38.                 allLines += "\n";
  39.             }
  40.             saveToFile(filePath, true, allLines); // Save the buffer in the file
  41.             allLines = ""; // Free the buffer to speed things up
  42.         }
  43.  
  44.     }
  45.  
  46.     static void saveToFile(String filePath, boolean append, String linesToWrite) {
  47.         try {
  48.             FileWriter fw = new FileWriter(new File(filePath), append);
  49.             fw.write(linesToWrite);
  50.             fw.close();
  51.         } catch (IOException e) {
  52.             e.printStackTrace();
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement