Search in sources :

Example 11 with Response

use of org.jsoup.Connection.Response in project Shadbot by Shadorc.

the class LyricsCmd method execute.

@Override
public void execute(Context context) throws MissingArgumentException {
    List<String> args = StringUtils.split(context.getArg(), 2, "-");
    if (args.size() != 2) {
        throw new MissingArgumentException();
    }
    LoadingMessage loadingMsg = new LoadingMessage("Loading lyrics...", context.getChannel());
    loadingMsg.send();
    try {
        String artistSrch = NetUtils.encode(args.get(0).replaceAll("[^A-Za-z0-9]", "-"));
        String titleSrch = NetUtils.encode(args.get(1).replaceAll("[^A-Za-z0-9]", "-"));
        // Make a direct search with the artist and the title
        String url = String.format("%s/lyrics/%s/%s", HOME_URL, artistSrch, titleSrch);
        Response response = NetUtils.getResponse(url);
        Document doc = NetUtils.getResponse(url).parse().outputSettings(PRESERVE_FORMAT);
        // If the direct search found nothing
        if (response.statusCode() == 404 || response.parse().text().contains("Oops! We couldn't find that page.")) {
            url = String.format("%s/search/%s-%s?", HOME_URL, artistSrch, titleSrch);
            // Make a search request on the site
            Document searchDoc = NetUtils.getDoc(url);
            Element trackListElement = searchDoc.getElementsByClass("tracks list").first();
            if (trackListElement == null) {
                loadingMsg.edit(TextUtils.noResult(context.getArg()));
                return;
            }
            // Find the first element containing "title" (generally the best result) and get its URL
            url = HOME_URL + trackListElement.getElementsByClass("title").attr("href");
            doc = NetUtils.getDoc(url).outputSettings(PRESERVE_FORMAT);
        }
        String artist = doc.getElementsByClass("mxm-track-title__artist").html();
        String title = StringUtils.remove(doc.getElementsByClass("mxm-track-title__track ").text(), "Lyrics");
        String albumImg = "https:" + doc.getElementsByClass("banner-album-image").select("img").first().attr("src");
        String lyrics = StringUtils.truncate(doc.getElementsByClass("mxm-lyrics__content ").html(), MAX_LYRICS_LENGTH);
        EmbedBuilder embed = EmbedUtils.getDefaultEmbed().setLenient(true).withAuthorName(String.format("Lyrics (%s - %s)", artist, title)).withAuthorUrl(url).withThumbnail(albumImg).appendDescription(url + "\n\n" + lyrics);
        loadingMsg.edit(embed.build());
    } catch (IOException err) {
        loadingMsg.delete();
        Utils.handle("getting lyrics", context, err);
    }
}
Also used : Response(org.jsoup.Connection.Response) EmbedBuilder(sx.blah.discord.util.EmbedBuilder) MissingArgumentException(me.shadorc.shadbot.exception.MissingArgumentException) Element(org.jsoup.nodes.Element) LoadingMessage(me.shadorc.shadbot.utils.object.LoadingMessage) IOException(java.io.IOException) Document(org.jsoup.nodes.Document)

Example 12 with Response

use of org.jsoup.Connection.Response in project ripme by RipMeApp.

the class UpdateUtils method downloadJarAndLaunch.

private static void downloadJarAndLaunch(String updateJarURL) throws IOException {
    Response response;
    response = Jsoup.connect(updateJarURL).ignoreContentType(true).timeout(Utils.getConfigInteger("download.timeout", 60 * 1000)).maxBodySize(1024 * 1024 * 100).execute();
    FileOutputStream out = new FileOutputStream(updateFileName);
    out.write(response.bodyAsBytes());
    out.close();
    logger.info("Download of new version complete; saved to " + updateFileName);
    // Setup updater script
    final String batchFile, script;
    final String[] batchExec;
    String os = System.getProperty("os.name").toLowerCase();
    if (os.contains("win")) {
        // Windows
        batchFile = "update_ripme.bat";
        String batchPath = new File(batchFile).getAbsolutePath();
        script = "@echo off\r\n" + "timeout 1" + "\r\n" + "copy " + updateFileName + " " + mainFileName + "\r\n" + "del " + updateFileName + "\r\n" + "ripme.jar" + "\r\n" + "del " + batchPath + "\r\n";
        batchExec = new String[] { batchPath };
    } else {
        // Mac / Linux
        batchFile = "update_ripme.sh";
        String batchPath = new File(batchFile).getAbsolutePath();
        script = "#!/bin/sh\n" + "sleep 1" + "\n" + "cd " + new File(mainFileName).getAbsoluteFile().getParent() + "\n" + "cp -f " + updateFileName + " " + mainFileName + "\n" + "rm -f " + updateFileName + "\n" + "java -jar \"" + new File(mainFileName).getAbsolutePath() + "\" &\n" + "sleep 1" + "\n" + "rm -f " + batchPath + "\n";
        batchExec = new String[] { "sh", batchPath };
    }
    // Create updater script
    BufferedWriter bw = new BufferedWriter(new FileWriter(batchFile));
    bw.write(script);
    bw.flush();
    bw.close();
    logger.info("Saved update script to " + batchFile);
    // Run updater script on exit
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        try {
            logger.info("Executing: " + batchFile);
            Runtime.getRuntime().exec(batchExec);
        } catch (IOException e) {
            // TODO implement proper stack trace handling this is really just intented as a placeholder until you implement proper error handling
            e.printStackTrace();
        }
    }));
    logger.info("Exiting older version, should execute update script (" + batchFile + ") during exit");
    System.exit(0);
}
Also used : Response(org.jsoup.Connection.Response) FileOutputStream(java.io.FileOutputStream) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 13 with Response

use of org.jsoup.Connection.Response in project ripme by RipMeApp.

the class Http method response.

public Response response() throws IOException {
    Response response = null;
    IOException lastException = null;
    int retries = this.retries;
    while (--retries >= 0) {
        try {
            response = connection.execute();
            return response;
        } catch (IOException e) {
            logger.warn("Error while loading " + url, e);
            lastException = e;
        }
    }
    throw new IOException("Failed to load " + url + " after " + this.retries + " attempts", lastException);
}
Also used : Response(org.jsoup.Connection.Response) IOException(java.io.IOException)

Example 14 with Response

use of org.jsoup.Connection.Response in project ripme by RipMeApp.

the class EroShareRipper method getURLs.

public static List<URL> getURLs(URL url) throws IOException {
    Response resp = Http.url(url).ignoreContentType().response();
    Document doc = resp.parse();
    List<URL> URLs = new ArrayList<>();
    // Pictures
    Elements imgs = doc.getElementsByTag("img");
    for (Element img : imgs) {
        if (img.hasClass("album-image")) {
            String imageURL = img.attr("src");
            imageURL = "https:" + imageURL;
            URLs.add(new URL(imageURL));
        }
    }
    // Videos
    Elements vids = doc.getElementsByTag("video");
    for (Element vid : vids) {
        if (vid.hasClass("album-video")) {
            Elements source = vid.getElementsByTag("source");
            String videoURL = source.first().attr("src");
            URLs.add(new URL("https:" + videoURL));
        }
    }
    return URLs;
}
Also used : Response(org.jsoup.Connection.Response) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) URL(java.net.URL)

Example 15 with Response

use of org.jsoup.Connection.Response in project ripme by RipMeApp.

the class FlickrRipper method signinToFlickr.

/**
 * Login to Flickr.
 * @return Cookies for logged-in session
 * @throws IOException
 */
@SuppressWarnings("unused")
private Map<String, String> signinToFlickr() throws IOException {
    Response resp = Jsoup.connect("http://www.flickr.com/signin/").userAgent(USER_AGENT).followRedirects(true).method(Method.GET).execute();
    Document doc = resp.parse();
    Map<String, String> postData = new HashMap<>();
    for (Element input : doc.select("input[type=hidden]")) {
        postData.put(input.attr("name"), input.attr("value"));
    }
    postData.put("passwd_raw", "");
    postData.put(".save", "");
    postData.put("login", new String(Base64.decode("bGVmYWtlZGVmYWtl")));
    postData.put("passwd", new String(Base64.decode("MUZha2V5ZmFrZQ==")));
    String action = doc.select("form[method=post]").get(0).attr("action");
    resp = Jsoup.connect(action).cookies(resp.cookies()).data(postData).method(Method.POST).execute();
    return resp.cookies();
}
Also used : Response(org.jsoup.Connection.Response) HashMap(java.util.HashMap) Element(org.jsoup.nodes.Element) Document(org.jsoup.nodes.Document)

Aggregations

Response (org.jsoup.Connection.Response)28 Document (org.jsoup.nodes.Document)12 IOException (java.io.IOException)11 Element (org.jsoup.nodes.Element)10 HashMap (java.util.HashMap)8 URL (java.net.URL)4 Connection (org.jsoup.Connection)4 Elements (org.jsoup.select.Elements)4 ArrayList (java.util.ArrayList)3 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 ParseException (java.text.ParseException)1 LocalDate (java.time.LocalDate)1 DateTimeParseException (java.time.format.DateTimeParseException)1 Map (java.util.Map)1 Matcher (java.util.regex.Matcher)1