net.landora.gsbliptv.BlipTV.java Source code

Java tutorial

Introduction

Here is the source code for net.landora.gsbliptv.BlipTV.java

Source

/**
 * Copyright 2012 Blake Dickie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.landora.gsbliptv;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.net.URL;
import net.landora.gsbliptv.data.BlipVideo;
import net.landora.gsbliptv.wrappers.GetVideoWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author bdickie
 */
public class BlipTV {

    // <editor-fold defaultstate="collapsed" desc="Singleton">
    /**
     * SingletonHolder is loaded on the first execution of Singleton.getInstance()
     * or the first access to SingletonHolder.instance , not before.
     */
    private static class SingletonHolder {

        private final static BlipTV instance = new BlipTV();
    }

    public static BlipTV getInstance() {
        return SingletonHolder.instance;
    }
    // </editor-fold>

    private Logger log = LoggerFactory.getLogger(getClass());

    private BlipTV() {
        mapper = new XmlMapper();
    }

    private ObjectMapper mapper;

    public BlipVideo getVideo(long videoId) {
        try {
            URL url = new URL(String.format("http://blip.tv/file/%d?skin=api", videoId));

            GetVideoWrapper readValue = mapper.readValue(url, GetVideoWrapper.class);
            if (readValue == null || !readValue.getStatus().equalsIgnoreCase("OK"))
                return null;

            return readValue.getPayload().get(0);
        } catch (Exception e) {
            log.error("Error getting BlipTV video info.", e);
            return null;
        }
    }

    public BlipVideo getVideoFromEmbedCode(String embedCode) {
        try {
            URL url = new URL(String.format("http://blip.tv/players/episode/%s?skin=api", embedCode));

            GetVideoWrapper readValue = mapper.readValue(url, GetVideoWrapper.class);
            if (readValue == null || !readValue.getStatus().equalsIgnoreCase("OK"))
                return null;

            return readValue.getPayload().get(0);
        } catch (Exception e) {
            log.error("Error getting BlipTV video info.", e);
            return null;
        }
    }

    public BlipVideo getCurrentChannelVideos(String channelName) {
        return getChannelVideos(channelName, 1);
    }

    public BlipVideo getChannelVideos(String channelName, int page) {
        try {
            URL url = new URL(String.format("http://blip.tv/%d?skin=api&page=%d", channelName, page));

            GetVideoWrapper readValue = mapper.readValue(url, GetVideoWrapper.class);
            if (readValue == null || !readValue.getStatus().equalsIgnoreCase("OK"))
                return null;

            return readValue.getPayload().get(0);
        } catch (Exception e) {
            log.error("Error getting BlipTV video info.", e);
            return null;
        }
    }

}