JDA doesn't support anymore attachments with downloadToFile()? - java

I write discord bot with using JDA and i have one important question how to download attachments or work with them? Because my IntelliJ say "Deprecated API usage" for method like attachment.downloadToFile("name.png);
So now we shouldn't download users files send in message? Or how we should do it in good way? I search a lot of wiki from JDA and different posts, but everywhere i didn't see, a new option to handle this download files, becasue all methods to download, are "Deprecated API" even method like "attachment.retrieveInputStream().join()" retrieveInputStream its too not good way :(
Search a lot on wiki/others pages for more information but nothing found :(

The deprecation notice says this:
Deprecated.
Replaced by getProxy(), see FileProxy.downloadToFile(File)
This means you should use attachment.getProxy().downloadToFile(File) instead.
Example:
attachment.getProxy().downloadToFile(new File("myimage.png")).thenAccept(file -> {
System.out.println("Written to file " + file.getAbsolutePath() + ".");
});
Or using NIO instead:
attachment.getProxy().downloadToPath().thenAccept(path -> {
System.out.println("Written to file " + path + ". Total size: " + Files.size(path));
});

Related

Convert ArrayList<Custom> to gson in Java Android

Good afternoon.
I can not understand why the code does not work after publishing in the Google market.
Before publishing in android studio code
ArrayList<TableAccount> tableAccount = getTableAccount();
String jsonStr = new Gson().toJson(tableAccount);
result:
Log.d(TAG, "RESULT: " + jsonStr);
RESULT: [{"name":"payment","valueFloat":0.0,"valueInt":0,"valueStr":"no"}]
But, after posting, I see:
Log.d(TAG, "RESULT: " + jsonStr);
RESULT: [{"a":"payment","b":"no","c":0,"d":0.0}]
Why do the letters a b c appear?
And where do the keys disappear? ["name", "valueFloat", "valueInt", "valueStr"]
Apparently, you turned on ProGuard/R8 for your release build, and its obfuscation mode renamed your fields.
The biggest lesson from here is to make sure that you test your release builds before uploading them to Google Play.
To fix the problem, you could:
Turn off ProGuard/R8 entirely,
Leave it on but disable obfuscation entirely,
Leave it on but disable obfuscation for these specific classes, or
Use #SerializedName annotations to teach Gson what names to use, instead of its default of using Java reflection to look up the names of the fields (as obfuscation renames those fields)

How to convert raw html to something I can test with Selenium?

My team has created a CMS. When it's API is called by the client (using POST - with parameters), it responds with raw HTML, which is then injected into the client's page.
I am assigned to create automated testing specifically for the HTML (not the client page). On my computer I can save the HTML in a file and open with a browser to test it out locally.
To get the test to run on a build server or through Sauce Labs, I am trying to figure out a way to render the HTML so I can have my test framework grab a screenshot to be compared. My test framework is Java/Junit using Selenium bindings, and I use Applitools for screenshot comparison.
I looked into PhantomJS but got a bit lost in the JS world (I am much more comfortable with Java). Also it appears that these artifacts are quite dated in Maven. If this is suggested, I would really appreciate an example.
I have found topics related to posting to the http endpoint using the Junit approach (leveraging Rest Assured), but I am stuck on what to do with the HTML response and how to plug that into a Selenium test. Please, can anyone offer guidance or suggest a tool to do this?
You could use the data scheme to load the html:
driver.get("data:text/html;charset=utf-8," + URLEncoder.encode(pageHtml, "UTF-8"));
Though you may be limited by the length and it won't load the resources present in a separate folder.
Another way would be to execute the requests directly in the page and to then rewrite the whole page with the result.
Something like:
// set domain
driver.get("https://stackoverflow.com");
// navigate some HTML from a request
navigate(driver, "POST", "/search", "q=abcd");
public void navigate(driver, method, path, body) {
String JS_NAVIGATE_REQUEST =
"(function(method, path, body, callback){ " +
" var xhr = new XMLHttpRequest(); " +
" xhr.open(method, path, true); " +
" xhr.onloadend = function(){ " +
" document.write(xhr.responseText); " +
" document.close(); " +
" callback(); " +
" }; " +
" document.write(''); " +
" xhr.send(body); " +
"}).apply(window, arguments); " ;
((JavascriptExecutor)driver).executeAsyncScript(JS_NAVIGATE_REQUEST, method, path, body);
}
I would save the HTML into a file and use IE to display that file.
You can use badboy tool to capture the screenshots of the HTML files saved locally but this is not including selenium integration. It is just to have the baseline screenshots from the HTML saved locally.
Save all the responses in HTML with some predefined naming convention like SS_1,SS_2.
Open badboy and pass the path of 1 file in the browsing pane (at upper right) or use "Request" from tools (Drag & Drop) and provide the path of first file saved locally.
Put snapshot tool below it and configure to save snapshots.
Add variables ${iterator} and provide values
Double click on "Request" and change the file name suffix from SS_1 to ${iterator}
Now, configure step to run for each value of variables by double clicking on the step and selecting the second radio button (For each value of variable) .
Reference tool - Badboy

Dropbox last modification with Java API

I am writing a Dropbox console application. I need to find last modification for my account. I can get file metadata with date of last modification, like this:
DbxEntry.WithChildren listing = client.getMetadataWithChildren(path);
for (DbxEntry child : listing.children) {
System.out.println(" " + child.name + ": " + child.toString());
}
But how can I find the latest modification for all of my folders?
The Dropbox API and the official Dropbox Java SDK don't expose a modified time for folders, but we'll consider it a feature request.
For files, you can access clientModified and serverModified on FileMetadata.
(Note that the code in your question uses the old, deprecated Java SDK, so you should switch to the new one.)

Get formatted output from eclipse template variable

How can I make an eclipse java template that allows generating java code that eases the repeating part of registering code for a java method. Example:
Assume that the class description is like so:
class A{
public static void methodName(String s, int i, Object o) {
}
}
Now, what I want is to make a template that does something somewhat like this:
"${enclosing_type}.${enclosing_method}(" + ${variable1} + ", " + ${variable2} + ", " + ${variable3} + ")"
Given the available Eclipse variables I know, the idea would probably be:
"${enclosing_type}.${enclosing_method}(" + ${enclosing_method_arguments(" + \", \" + ")} + ")"
Where that argument would signal the glue to the join of each element of the enclosing_method_arguments. The result of the format would be:
"A.methodName(" + s + ", " + i + ", " + o + ")"
If there's an even better alternative, I'm open for suggestions.
This is meant to be used with a piece of code that is executed a LOT,
Unfortunately, String.format() (and related) "solution" is not an option here due to the requirement above and due to other inherited requirements with what I'm working on. It must generate that code in that format no matter what, unfortunately.
I'm open to any plugins that allow that and, if eclipse doesn't have it, I'm open to make a plugin myself... In case of me having to do a plugin please do show me the resources required to make it.
It should be possible to write a custom variable resolver. It is defined by org.eclipse.ui.editors.templates extension point
You could implement a custom resolver that points to Java context (its id is java defined in jdt.ui).
I don't know any plugins that would provide what you need out of the box.
If you are new to Eclipse plug-ins you might need to read the documentation on extending Eclipse. However, the task is really simple, so you should be able to do it after just a glimpse over the docs.

Java API or 3rdparty for getting duration of movie

I've many video files. All the types are different (mkv, avi, mp4, flv, etc.).
I want to iterate these files and get the duration of the movies.
Currently I use vlcj which is OK, but the code is a little bit confusing me.
I have to play the media before I can get the length. Then stop the media. This is weird, isn't it.
Can I do that better somehow? I've heard about xuggler, but the development has stopped.
I need an API or 3rdparty component that is still alive. And I don't want to play the media before I play :)
Finally, I've solved the problem with Xuggle.
After setup I had to add these lines, and it works fine:
IContainer container = IContainer.make();
if (container.open(movie, IContainer.Type.READ, null) < 0) {
throw new RuntimeException("Cannot open '" + movie + "'");
}
logger.info("# Duration (ms): " + ((container.getDuration() == Global.NO_PTS) ? "unknown" : "" + container.getDuration() / 100));
logger.info("# File size (bytes): " + container.getFileSize());
logger.info("# Bit rate: " + container.getBitRate());
BTW, the setup was awful, but finally... got this code to work.
See Java Media Framework.
http://docs.oracle.com/cd/E17802_01/j2se/javase/technologies/desktop/media/jmf/2.1.1/apidocs/index.html
Here you will find links to other similar projects also http://en.wikipedia.org/wiki/Java_Media_Framework

Categories

Resources