Visualizzazione post con etichetta java. Mostra tutti i post
Visualizzazione post con etichetta java. Mostra tutti i post

sabato 10 settembre 2011

cercare data più vicina in una lista di date

Immaginate di avere una lista ordinata di date.
Immaginate di dover trovare la data nella lista + vicina alla data proposta.


  • Utilizziamo un TreeSet per le funzioni lower e higher (per trovare la data nella lista che la precede e la data nella lista successiva) e joda-time per calcolare gli intervalli tra le date
  • calcoliamo la distanza tra la data  proposta e le due date precedente e successivo
  • restituiamo come risultato la data che genera intervallo di minore durata.
Ecco l'esempio intero...si capisce al volo.. [nel codice vanno gestite le eccezioni ...es. lista date vuota...]



package by.giava.mytrips.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TreeSet;

import org.joda.time.DateTime;
import org.joda.time.Interval;

public class DateTree {
private TreeSet<Date> treeset;

public DateTree() {
this.treeset = new TreeSet<Date>();
}

public void reset() {
this.treeset = new TreeSet<Date>();
}

public void add(Date data) {
getTreeset().add(data);
}

public TreeSet<Date> getTreeset() {
return treeset;
}

public void setTreeset(TreeSet<Date> treeset) {
this.treeset = treeset;
}

public Date nearest(Date data) {
DateTime dataF = new DateTime(data);

Date dataMin = getTreeset().lower(data);
DateTime dataMinJ = new DateTime(dataMin);

Date dataMax = getTreeset().higher(data);
DateTime dataMaxJ = new DateTime(dataMax);

Interval periodMin = new Interval(dataMinJ, dataF);
Interval periodMax = new Interval(dataF, dataMaxJ);

if (periodMin.toDurationMillis() <= periodMax.toDurationMillis()) {
return dataMin;
} else {
return dataMax;
}
}

public static void main(String[] args) throws ParseException {
SimpleDateFormat form = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateTree dat = new DateTree();
dat.add(form.parse("01-01-2011 10:30:00"));
dat.add(form.parse("01-01-2011 10:50:00"));
dat.add(form.parse("01-01-2011 11:00:00"));

Date result = dat.nearest(form.parse("01-01-2011 10:56:00"));
System.out.println(result);
}
}

domenica 31 luglio 2011

instagram from java

se avete un iphone e vi piace fare foto, non potetet non provare instagram..l'ultimo ritrovato dello scatto istantaneo (stile polaroid) solo per iphone (questo non piace...) con una serie di filtri per abbellire, rendere + strane le vs immagini...

a noi interessa usare le api da java...
è possibile soltanto il recupero delle foto/informazioni/commenti/amici...
non si può caricare nulla....

ho provato questa libreria, ancora poco organizzata
https://github.com/sachin-handiekar/jInstagram

(va checkoutata e compilata ion locale, non ci sono librerie..no maven...no ant)...


una guida minimale per muovere i primi passi:
https://github.com/sachin-handiekar/jInstagram/wiki/jInstagram-Usage

per fare i test vi serve la registrazione sul sito:
http://instagram.com/developer/

per fare dei test da client (eclipse), fate cosi:
1) create una paginetta (che magari scrive solo ciao!) su un server pubblico:

basta che sia raggiungibile:
es http://giava.by/instaflower/oauth/index.html

2) da browser chiamate questo indirizzo:
http://instagram.com/oauth/authorize/?client_id=CLIENT_ID&redirect_uri=REDIRECT-URI&response_type=token

sostituite nell'url il CLIENT_ID che avete ricevuto nel registrarvi sul sito sviluppatori e il vs indirizzo pubblico (REDIRECT-URI)

3) prendete il token che vi è stato assegnato:

es: http://giava.by/instaflower/oauth/#access_token=xxxxxx.xxx.1926f93543824d95bb83d3a9805874df

access_token=xxxxxx.xxx.1926f93543824d95bb83d3a9805874df

ecco un esempio rapido per avere tutte le vs foto:

import java.util.List;

import org.jinstagram.Instagram;
import org.jinstagram.auth.model.Token;
import org.jinstagram.entity.common.Caption;
import org.jinstagram.entity.common.Comments;
import org.jinstagram.entity.common.ImageData;
import org.jinstagram.entity.common.Images;
import org.jinstagram.entity.common.Likes;
import org.jinstagram.entity.common.Location;
import org.jinstagram.entity.users.basicinfo.UserInfo;
import org.jinstagram.entity.users.basicinfo.UserInfoData;
import org.jinstagram.entity.users.feed.MediaFeed;
import org.jinstagram.entity.users.feed.MediaFeedData;
import org.jinstagram.entity.users.feed.UserFeed;
import org.jinstagram.entity.users.feed.UserFeedData;
import org.jinstagram.exceptions.InstagramException;

public class First {

static String your_client_secret = "172460d1932b4393b2097f67dfd78ff1";
static String access_token="xxxxxx.xxx.1926f93543824d95bb83d3a9805874df";
static String nickname ="pippoPluto";

public static void main(String[] args) throws InstagramException {

Token accessToken = new Token(
access_token,
your_client_secret);
Instagram instagram = new Instagram(accessToken);
UserFeed userFeed = instagram.searchUser(nickname);
List userList = userFeed.getUserList();
long userId = 0;
for (UserFeedData userFeedData : userList) {
userId = userFeedData.getId();
}
UserInfo userInfo = instagram.getUserInfo(userId);

UserInfoData userData = userInfo.getData();
System.out.println("id : " + userData.getId());
System.out.println("first_name : " + userData.getFirst_name());
System.out.println("last_name : " + userData.getLast_name());
System.out
.println("profile_picture : " + userData.getProfile_picture());
System.out.println("website : " + userData.getWebsite());

MediaFeed mediaFeed = instagram.getUserFeeds();
List mediaFeeds = mediaFeed.getData();

for (MediaFeedData mediaData : mediaFeeds) {
System.out.println("id : " + mediaData.getId());
System.out.println("created time : " + mediaData.getCreatedTime());
System.out.println("link : " + mediaData.getLink());
System.out.println("tags : " + mediaData.getTags().toString());
System.out.println("filter : " + mediaData.getImageFilter());
System.out.println("type : " + mediaData.getType());

System.out.println("-- Comments --");
Comments comments = mediaData.getComments();

System.out.println("-- Caption --");
Caption caption = mediaData.getCaption();

System.out.println("-- Likes --");
Likes likes = mediaData.getLikes();

System.out.println("-- Images --");
Images images = mediaData.getImages();

ImageData lowResolutionImg = images.getLowResolution();
ImageData highResolutionImg = images.getStandardResolution();
ImageData thumbnailImg = images.getThumbnail();

Location location = mediaData.getLocation();
if (location != null)
System.out.println(location.getName());
System.out.println("****************************************");
}

}
}