<commit_before>package br.com.adley.tvmaze;

import android.net.Uri;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import br.com.adley.library.DownloadStatus;
import br.com.adley.library.GetRawData;
import br.com.adley.library.TVShow;

/**
 * Created by Adley.Damaceno on 09/03/2016.
 * Download data from TvShow.
 */

public class GetTVShowJsonData extends GetRawData {

    private String LOG_TAG = GetTVShowJsonData.class.getSimpleName();
    private List<TVShow> tvshows;
    private Uri destinationUri;

    public GetTVShowJsonData(String showToSearch) {
        super(null);
        createAndUpdateUri(showToSearch);
    }

    public  void execute(){
        super.setRawUrl(destinationUri.toString());
        DownloadJsonData downloadJsonData = new DownloadJsonData();
        Log.v(LOG_TAG, "Built Uri = "+ destinationUri.toString());
        //downloadJsonData.execute(destinationUri.toString());
    }
    public boolean createAndUpdateUri(String showToSearch) {
        final String TVMAZE_API_BASE_URL = "http://api.tvmaze.com/search/shows";

        destinationUri = Uri.parse(TVMAZE_API_BASE_URL).buildUpon()
                .appendQueryParameter("q", showToSearch).build();
        return destinationUri != null;
    }

    public void processResult() {
        if (getDownloadStatus() != DownloadStatus.OK) {
            Log.e(LOG_TAG, "Error download raw file");
            return;
        }

        final String ID_TVSHOW = "id";
        final String URL_TVSHOW = "url";
        final String NAME_TVSHOW = "name";
        final String TYPE_TVSHOW = "type";
        final String LANGUAGE_TVSHOW = "language";
        final String STATUS_TVSHOW = "status";
        final String SUMMARY_TVSHOW = "summary";
        final String IMAGE_TVSHOW = "image";
        final String IMAGE_MEDIUM_TVSHOW = "medium";
        final String IMAGE_ORIGINAL_TVSHOW = "original";
        final String LINKS_TVSHOW = "_links";
        final String PREVIOUS_EPISODE_TVSHOW = "previousepisode";
        final String NEXT_EPISODE_TVSHOW = "nextepisode";
        final String HREF_EPISODES_TVSHOW = "href";

        try {
            // Navigate and parse the JSON Data
            JSONArray jsonArray = new JSONArray(getData());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonobject = jsonArray.getJSONObject(i);
                JSONObject oneShowJsonObject = new JSONObject(jsonobject.toString());
                JSONObject showJSonObject = oneShowJsonObject.getJSONObject("show");
                String id = showJSonObject.getString(ID_TVSHOW);
                String url = showJSonObject.getString(URL_TVSHOW);
                String name = showJSonObject.getString(NAME_TVSHOW);
                String type = showJSonObject.getString(TYPE_TVSHOW);
                String language = showJSonObject.getString(LANGUAGE_TVSHOW);
                String status = showJSonObject.getString(STATUS_TVSHOW);
                String summary = showJSonObject.getString(SUMMARY_TVSHOW);

                // Images - Posters
                JSONObject jsonImageData = showJSonObject.getJSONObject(IMAGE_TVSHOW);
                String imageMedium = jsonImageData.getString(IMAGE_MEDIUM_TVSHOW);
                String imageOriginal = jsonImageData.getString(IMAGE_ORIGINAL_TVSHOW);

                JSONObject linkJsonData = new JSONObject(LINKS_TVSHOW);
                // Previous Episode
                JSONObject previousEpisodeData = new JSONObject(PREVIOUS_EPISODE_TVSHOW);
                String previousEpisode = previousEpisodeData.getString(HREF_EPISODES_TVSHOW);

                // Next Episode
                JSONObject nextEpisodeData = new JSONObject(NEXT_EPISODE_TVSHOW);
                String nextEpisode = nextEpisodeData.getString(HREF_EPISODES_TVSHOW);

                // Create TVShow Object and add to the List of Shows
                TVShow TVShowObject = new TVShow(id, url, name, type, language, status, imageMedium, imageOriginal, summary, previousEpisode, nextEpisode);
                this.tvshows.add(TVShowObject);
            }
            for (TVShow singleShow: tvshows){
                Log.v(LOG_TAG, singleShow.toString());
            }
<commit_msg>Fixed error when the show does not have any previous or next episode<commit_after>package br.com.adley.tvmaze;

import android.net.Uri;
import android.util.Log;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import br.com.adley.library.DownloadStatus;
import br.com.adley.library.GetRawData;
import br.com.adley.library.TVShow;

/**
 * Created by Adley.Damaceno on 09/03/2016.
 * Download data from TvShow.
 */

public class GetTVShowJsonData extends GetRawData {

    private String LOG_TAG = GetTVShowJsonData.class.getSimpleName();
    private List<TVShow> tvshows;
    private Uri destinationUri;

    public GetTVShowJsonData(String showToSearch) {
        super(null);
        createAndUpdateUri(showToSearch);
    }

    public  void execute(){
        super.setRawUrl(destinationUri.toString());
        DownloadJsonData downloadJsonData = new DownloadJsonData();
        Log.v(LOG_TAG, "Built Uri = "+ destinationUri.toString());
        downloadJsonData.execute(destinationUri.toString());
    }
    public boolean createAndUpdateUri(String showToSearch) {
        final String TVMAZE_API_BASE_URL = "http://api.tvmaze.com/search/shows";

        destinationUri = Uri.parse(TVMAZE_API_BASE_URL).buildUpon()
                .appendQueryParameter("q", showToSearch).build();
        return destinationUri != null;
    }

    public void processResult() {
        if (getDownloadStatus() != DownloadStatus.OK) {
            Log.e(LOG_TAG, "Error download raw file");
            return;
        }

        final String ID_TVSHOW = "id";
        final String URL_TVSHOW = "url";
        final String NAME_TVSHOW = "name";
        final String TYPE_TVSHOW = "type";
        final String LANGUAGE_TVSHOW = "language";
        final String STATUS_TVSHOW = "status";
        final String SUMMARY_TVSHOW = "summary";
        final String IMAGE_TVSHOW = "image";
        final String IMAGE_MEDIUM_TVSHOW = "medium";
        final String IMAGE_ORIGINAL_TVSHOW = "original";
        final String LINKS_TVSHOW = "_links";
        final String PREVIOUS_EPISODE_TVSHOW = "previousepisode";
        final String NEXT_EPISODE_TVSHOW = "nextepisode";
        final String HREF_EPISODES_TVSHOW = "href";

        try {
            // Navigate and parse the JSON Data
            tvshows = new ArrayList<TVShow>();
            JSONArray jsonArray = new JSONArray(getData());
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonobject = jsonArray.getJSONObject(i);
                JSONObject oneShowJsonObject = new JSONObject(jsonobject.toString());
                JSONObject showJSonObject = oneShowJsonObject.getJSONObject("show");
                String id = showJSonObject.getString(ID_TVSHOW);
                String url = showJSonObject.getString(URL_TVSHOW);
                String name = showJSonObject.getString(NAME_TVSHOW);
                String type = showJSonObject.getString(TYPE_TVSHOW);
                String language = showJSonObject.getString(LANGUAGE_TVSHOW);
                String status = showJSonObject.getString(STATUS_TVSHOW);
                String summary = showJSonObject.getString(SUMMARY_TVSHOW);

                // Images - Posters
                JSONObject jsonImageData = showJSonObject.getJSONObject(IMAGE_TVSHOW);
                String imageMedium = jsonImageData.getString(IMAGE_MEDIUM_TVSHOW);
                String imageOriginal = jsonImageData.getString(IMAGE_ORIGINAL_TVSHOW);
                String previousEpisode;
                String nextEpisode;
                try {
                    // Previous Episode
                    JSONObject linkJsonData = showJSonObject.getJSONObject(LINKS_TVSHOW);
                    JSONObject previousEpisodeData = linkJsonData.getJSONObject(PREVIOUS_EPISODE_TVSHOW);
                    previousEpisode = previousEpisodeData.getString(HREF_EPISODES_TVSHOW);
                } catch (Exception e){
                    previousEpisode = null;
                    Log.e(LOG_TAG, "Do not have previous episodes. The error is: "+ e);
                }
                try{
                    // Next Episode
                    JSONObject linkJsonData = showJSonObject.getJSONObject(LINKS_TVSHOW);
                    JSONObject nextEpisodeData = linkJsonData.getJSONObject(NEXT_EPISODE_TVSHOW);
                    nextEpisode = nextEpisodeData.getString(HREF_EPISODES_TVSHOW);
                }catch (Exception e){
                    nextEpisode = null;
                    Log.e(LOG_TAG, "Do not have next episode yet. The error is: "+ e);
                }

                // Create TVShow Object and add to the List of Shows
                TVShow tvShowObject = new TVShow(id, url, name, type, language, status, imageMedium, imageOriginal, summary, previousEpisode, nextEpisode);
                tvshows.add(tvShowObject);
            }
            for (TVShow singleShow: tvshows){
                Log.v(LOG_TAG, singleShow.toString());
            }

{'fraction_non_alphanumeric': 0.08526966531656363, 'fraction_numerical': 0.0019185674696226818, 'mean_word_length': 2.4470977222630417, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3433855', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2587, 'n_words': 568}