Chunked uploading of media with Twitter4J - java

I am trying to upload a video on Twitter. I am using the following code:
private UploadedMedia uploadMediaChunkedInit(long size) throws TwitterException {
return new UploadedMedia(post(
conf.getUploadBaseURL() + "media/upload.json",
new HttpParameter[] { new HttpParameter("command", CHUNKED_INIT),
new HttpParameter("media_type", "video/mp4"),
new HttpParameter("total_bytes", size) }).asJSONObject());
}
Where I am getting following error:
Method post is not defined
I got this code from here:
https://github.com/yusuke/twitter4j/pull/226/commits/73b43c1ae4511d3712118f61f983bcbca4ef0c59

Those methods are private implementation details/utilities of the TwitterImpl class.
See lines 1844-1860 and lines 1862-1878
(notice they are private).
You are approaching this incorrectly. Never should you have to call an internal implementation detail. Instead, use the public API of Twitter4J. You should use the uploadMediaChunked method instead. There is currently no javadoc I can link you to (since it's a relatively new feature).
I also see that this feature is not yet released. For now, you can build Twitter4J yourself and then use its public API (instead of copying source code into your own project).

Related

How to get the json representation of the ElasticSearch query/request that was built with Java API Client (7.16)?

A new Java API Client was released in the 7.16 version of ES and the Java Rest Client was deprecated. There was the ability to convert a query to JSON in the deprecated client. It was convenient for debugging/tuning/profiling of the query in Kibana.
Unfortunately, I don't see any mention of this in the new client documentation. So my question is:
Is it possible to get a JSON representation of the query that was constructed via the Java API Client? It can be either some utility class or log config of the client that prints outgoing requests.
It looks like this feature will be directly supported in an upcoming version: https://discuss.elastic.co/t/elaticsearch-java-client-output-dsl/300952
In the meantime, there is a workaround given in the above discussion, but it does not quite work. Here is a snippet that works for me:
StringWriter writer = new StringWriter();
JsonGenerator generator = JacksonJsonProvider.provider().createGenerator(writer);
request.serialize(generator, new JacksonJsonpMapper());
generator.flush();
return writer.toString();
The only way I found so far is to put a conditional breakpoint in
co/elastic/clients/transport/rest_client/RestClientTransport.java:215 from elasticsearch-java-7.16.2.jar and check the value of baos variable.
As a temporary method waiting for pull request mentionned below, i just used
// TODO delete when https://github.com/elastic/elasticsearch-java/pull/213
public static String serializeToString(Query q) {
return q._get().toString();
}
It does not print anything pretty but it covers my use case of string comparison for tests.

setContextPath() not found

I am trying to use the library Porcupine on Android. I have now downloaded a Rhino context file (.rhn) but can't find a way how to implement that file into my code.
The documentation says that I should use setContextPath() but this gives me the error:
Cannot resolve method 'setContextPath' in 'Builder'
That's how I tried it:
try {
porcupineManager = new PorcupineManager.Builder()
.setModelPath("porcupine_params_de.pv")
.setAccessKey(ACCESS_KEY)
.setKeywordPath("my_file_de_android_v2_0_0.ppn")
.setContextPath("my_file_de_android_v2_0_0.rhn");
.setSensitivity(0.7f).build(
getApplicationContext(),
porcupineManagerCallback);
porcupineManager.start();
}
...
I don't know what this method is good for since I can't even find it in its class.
So how can I use my .rhn file?
In the sample code from the link you provide, this method is called on different class:
PicovoiceManager picovoiceManager = new PicovoiceManager.Builder()
.setAccessKey("${ACCESS_KEY}")
.setKeywordPath("${KEYWORD_FILE_PATH}")
.setWakeWordCallback(wakeWordCallback)
.setContextPath("${CONTEXT_FILE_PATH}")
.setInferenceCallback(inferenceCallback)
.build(context);
you use PorcupineManager while sample code uses PicovoiceManager.

AndroidTwitterLogin.run crashes in Log.i line NoSuchMethodError

I am trying to get a simple Android app to work for Twitter.
I am using the library jtwitter.
I am trying to use the AndroidTwitterLogin class.
I have a button to authenticate using OAuth. It calls this code.
public void authorizeApp(View v) {
AndroidTwitterLogin atl = new AndroidTwitterLogin(this,
MY_TWITTER_KEY,MY_TWITTER_SECRET,MY_TWITTER_CALLBACK) {
protected void onSuccess(Twitter jtwitter, String[] tokens) {
jtwitter.setStatus("I can now post to Twitter using my android app !");
Log.i("TwitterOAuth","successfully authorized app");
authorizeText.setText("Authorized");
// Recommended: store tokens in your app for future use
// with the constructor OAuthSignpostClient(String consumerKey, String consumerSecret, String accessToken, String accessTokenSecret)
}
};
atl.run();
}
At runtime when I get to the AndroidTwitterLogin.run line it crashes with the following code.
Caused by: java.lang.NoSuchMethodError: No static method i(Ljava/lang/String;Ljava/lang/String;)V in class Landroid/util/Log; or its super classes (declaration of 'android.util.Log' appears in /system/framework/framework.jar)
at winterwell.jtwitter.android.AndroidTwitterLogin.run(AndroidTwitterLogin.java:78)
at com.franksapps.twitteroauth.TwitterActivity.authorizeApp(TwitterActivity.java:45)`
Does anyone know if this class is useable in jtwitter/Android or what to do to make this useable?
Maybe check which libraries you're including in your app?
The JTwitter download contains a dummy library, which is used to allow it to compile with or without Android. You want to avoid (maybe just delete) the following files: android.jar, android-dummy.jar. If one of them got included by accident, then it would cause issues.
NB: I confess I haven't used this particular class for a couple of years.
Daniel

Problems with Kontakt.io Beacon example

I have recently acquired a beacon from Kontakt.io
I followed the "Monitoring code example" from here: http://docs.kontakt.io/android-sdk/quickstart/#monitoring-code-sample
The Kontakt library is included (since other methods from the library is working), but the following line is giving me an error:
beaconManager.startMonitoring(Region.EVERYWHERE);
Error:
startMonitoring (java.util.Set<com.kontakt.sdk.android.device.Region>) in BeaconManager
cannot be applied to (com.kontakt.sdk.android.device.Region)
I have also tried creating a new region with the proximity uuid from the beacon:
static UUID uid = UUID.fromString("1DEFF9522D014664BB6088F065302B83");
private static final Region beacon = new Region(uid, 49668, 35726, null);
but this throws the same error.
I'm quite new to java and this might be a fairly simple question, but how do i resolve this?
If you check the changelog on their website you will see that there is new version of API. Clearly example is not updated. If you want to monitor Region.EVERYWHERE you can simply call beaconManager.startMonitoring() according to docs it should work.

Notification when file download completes in Play framework

I'm successfully using Play 1.2.4 to serve large binary file downloads to users using the renderBinary() method.
I'd like to have a hint of when the user actually completes the download. Generally speaking, I know this is somewhat possible as I've done it before. In an old version of my website, I wrote a simple servlet that served up binary file downloads. Once that servlet finished writing out the contents of the file, a notification was sent. Certainly not perfect, but useful nonetheless. In my testing, it did provide an indication of how long the user took to download a file.
Reviewing the Play source, I see that the play.mvc.results.RenderBinary class has a handy apply() method that I could use. I wrote my own version of RenderBinary so I could send the notification after the apply() method finished writing out the file contents.
The problem I found is that calls to response.out.write() obviously cache the outgoing bytes (via Netty?), so even though I am writing out several megabytes of data, the calls to play.mvc.Http.Response.out.write() complete in seconds, even though it takes the downloader a couple minutes to download the file.
I don't mind writing custom classes, although I'd prefer to use a stock Play 1.2.4 distribution.
Any ideas on how to get a notification of when the end of a file download is pushed out towards the user's browser?
It seems this may help you, as it tackles a somehow similar problem:
Detect when browser receives file download
I'm not sure you'll eb able to do it via renderBinary nor an #After annotation in the controller. Some browser-side detection of the download and then a notification to the server (pinging the download's end) would work.
There may be an alternative: using WebSockets (streaming the file via the socket and then having teh client to answer) but it may be overkill for this :)
you can use ArchivedEventStream.
first create a serializable ArcivedEventStream class..
public class Stream<String> extends ArchivedEventStream<String> implements Serializable{
public Stream(int arg0) {
super(arg0);
}
}
then on your controller...
public static void downloadPage(){
Stream<String> userStream = Cache.get(session.getId(),Stream.class);
if( userStream == null){
userStream = new Stream<String>(5);
Cache.add(session.getId(), userStream);
}
render();
}
public static void download(){
await(10000);// to provide some latency. actually no needed
renderBinary(Play.getFile("yourfile!"));
}
public static void isDownloadFinished(){
Stream<String> userStream = Cache.get(session.getId(),Stream.class);
List<IndexedEvent<String>> list = await(userStream.nextEvents(0));
renderJSON(list.get(0).data);
}
#After(only="download")
static void after(){
Stream<String> userStream = Cache.get(session.getId(),Stream.class);
userStream.publish("ok");
}
on your html...
#{extends 'main.html' /}
#{set title:'downloadPage' /}
download
<script>
$(document).ready(function(){
$.ajax('/application/isDownloadFinished',{success:function(data){
if(data){
console.log("downloadFinished");
}
}});
});
</script>
when your download finished, the original page will retrieve the notification.
This code is just a sample. You could read the api of ArchivedEventStream and make your own implementation..

Categories

Resources