Ejemplo n.º 1
0
  private static void startStream(List<String> terms) throws TwitterException {
    if (twitter != null) {
      twitter.cleanUp();
    }
    if (esClient != null) {
      esClient.close();
      esClient = null;
    }

    play.Configuration pconf = Play.application().configuration();
    String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name");
    if (elasticSearchCluster != null) {
      Logger.info("Configuring ElasticSearch...");
      Settings settings =
          ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build();

      esClient =
          new TransportClient(settings)
              .addTransportAddress(
                  new InetSocketTransportAddress(
                      pconf.getString("tweet.elasticsearch.transport.host"),
                      pconf.getInt("tweet.elasticsearch.transport.port")));
    } else {
      esClient = null;
    }

    twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration();
    TwitterStreamFactory tf = new TwitterStreamFactory(tconf);
    twitter = tf.getInstance();
    StatusListener l =
        new TweetListener(
            terms,
            esClient,
            pconf.getString("tweet.elasticsearch.index"),
            pconf.getString("tweet.elasticsearch.type"));
    twitter.addListener(l);

    String[] tracks = new String[terms.size()];
    StringBuffer termsString = new StringBuffer();
    for (int i = 0; i < terms.size(); i++) {
      tracks[i] = terms.get(i);
      if (i != 0) termsString.append(",");
      termsString.append(terms.get(i));
    }
    FilterQuery q = new FilterQuery().track(tracks);
    twitter.filter(q);
    Logger.info("Starting listening for tweets using terms " + termsString.toString() + "...");
  }
public class Application extends Controller {

  public static final File FILE_FOLDER = Play.getFile("public/pictures/");

  public static final String THUMBNAILS = "thumbnail";
  public static final String FULLSIZE = "fullsize";
  public static final String RESIZED = "resized";

  @Util
  public static List<Gallery> getGalleries() {
    return Gallery.all().fetch();
  }

  @Util
  public static List<Gallery> getPicasaGalleries() {
    return PicasaGallery.all().fetch();
  }

  public static void index() {
    if (Gallery.count() > 0) {
      Gallery defaultGallery = Gallery.all().first();
      showGallery(defaultGallery.id, defaultGallery.name);
    } else {
      render();
    }
  }

  public static void showImage(Long id) {
    Content content = Content.findById(id);
    notFoundIfNull(content);
    response.setContentTypeIfNotSet(content.image.type());
    File file = content.image.getFile();
    notFoundIfNull(file);
    renderBinary(file);
  }

  public static void showGallery(Long id, String name) {
    notFoundIfNull(id);
    Gallery gallery = Gallery.findById(id);
    notFoundIfNull(gallery);

    List<Picture> pictures = gallery.getPictures();
    List<ImageView> images = new ArrayList<ImageView>();
    for (Picture picture : pictures) images.add(picture.toImageView());

    render("Application/gallery.html", images, gallery);
  }

  public static void showPicasaGallery(Long id, String name) {
    notFoundIfNull(id);
    PicasaGallery gallery = PicasaGallery.findById(id);
    notFoundIfNull(gallery);

    PicasawebService service = new PicasawebService("portfolio");
    List<PhotoEntry> photoEntries = Collections.emptyList();
    try {
      java.net.URL feedUrl = new java.net.URL(gallery.getFeedUrl());

      AlbumFeed feed = service.getFeed(feedUrl, AlbumFeed.class);
      photoEntries = feed.getPhotoEntries();
    } catch (MalformedURLException e) {
      Logger.error("Service URL for Picasa is not well formed");
      e.printStackTrace();
    } catch (IOException e) {
      Logger.error("Error I/O while communicating with Picasa Service");
      e.printStackTrace();
    } catch (ServiceException e) {
      Logger.error("Picasa service error");
      e.printStackTrace();
    }

    List<ImageView> images = new ArrayList<ImageView>();
    for (PhotoEntry entry : photoEntries) {
      ImageView image = new ImageView();
      // We take the largest
      image.thumbnail =
          entry.getMediaThumbnails().get(entry.getMediaThumbnails().size() - 1).getUrl();
      image.url = entry.getMediaContents().get(0).getUrl();
      images.add(image);
    }

    render("Application/gallery.html", images, gallery);
  }

  public static void showContents() {
    List<Content> contents = Content.findAll();
    render(contents);
  }

  public static void thumbnail(Long galleryId, Long pictureId) {
    File picture = Picture.getFile(galleryId, pictureId, Application.THUMBNAILS);
    if (!picture.exists()) notFound();
    renderBinary(picture);
  }

  public static void resized(Long galleryId, Long pictureId) {
    File picture = Picture.getFile(galleryId, pictureId, Application.RESIZED);
    if (!picture.exists()) notFound();
    renderBinary(picture);
  }
}