<commit_before>package com.oakesville.mythling.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

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

import android.util.Log;

import com.oakesville.mythling.BuildConfig;
import com.oakesville.mythling.app.AppSettings;
import com.oakesville.mythling.media.Category;
import com.oakesville.mythling.media.Item;
import com.oakesville.mythling.media.LiveStreamInfo;
import com.oakesville.mythling.media.MediaList;
import com.oakesville.mythling.media.MediaSettings;
import com.oakesville.mythling.media.Movie;
import com.oakesville.mythling.media.Recording;
import com.oakesville.mythling.media.StorageGroup;
import com.oakesville.mythling.media.TvEpisode;
import com.oakesville.mythling.media.TvShow;
import com.oakesville.mythling.media.Video;
import com.oakesville.mythling.media.MediaSettings.MediaType;
import com.oakesville.mythling.media.MediaSettings.MediaTypeDeterminer;
import com.oakesville.mythling.media.MediaSettings.SortType;

public class MythTvParser implements MediaListParser
{
  private static final String TAG = MythTvParser.class.getSimpleName();

  private String json;
  private AppSettings appSettings;
  
  public MythTvParser(String json, AppSettings appSettings)
  {
    this.json = json;
    this.appSettings = appSettings;
  }
  
  public MediaList parseMediaList(MediaType mediaType) throws JSONException, ParseException
  {
    return parseMediaList(mediaType, null, null);
  }

  public MediaList parseMediaList(MediaType mediaType, StorageGroup storageGroup, String basePath) throws JSONException, ParseException
  {
    MediaList mediaList = new MediaList();
    mediaList.setMediaType(mediaType);
    mediaList.setStorageGroup(storageGroup);
    mediaList.setBasePath(basePath);
    long startTime = System.currentTimeMillis();
    JSONObject list = new JSONObject(json);
    SortType sortType = appSettings.getMediaSettings().getSortType(); 
    if (list.has("VideoMetadataInfoList"))
    {
      // videos, movies, or tvSeries
      MediaSettings mediaSettings = appSettings.getMediaSettings();
      JSONObject infoList = list.getJSONObject("VideoMetadataInfoList");
      mediaList.setRetrieveDate(parseMythDateTime(infoList.getString("AsOf")));
      mediaList.setCount(infoList.getString("Count"));
      JSONArray vids = infoList.getJSONArray("VideoMetadataInfos");
      
      String[] movieDirs = appSettings.getMovieDirs();
      String[] tvSeriesDirs = appSettings.getTvSeriesDirs();
      String[] vidExcludeDirs = appSettings.getVidExcludeDirs();

      for (int i = 0; i < vids.length(); i++)
      {
        JSONObject vid = (JSONObject) vids.get(i);
        MediaType type = MediaType.videos;
        // determine type
        if (mediaSettings.getTypeDeterminer() == MediaTypeDeterminer.directories)
        {
          if (vid.has("FileName"))
          {
            String filePath = vid.getString("FileName");
            if (mediaList.getStorageGroup() == null && filePath.startsWith(basePath + "/"))
              filePath = filePath.substring(basePath.length() + 1);
            
            for (String movieDir : movieDirs)
            {
              if (filePath.startsWith(movieDir))
              {
                type = MediaType.movies;
                break;
              }
            }
            for (String tvDir : tvSeriesDirs)
            {
              if (filePath.startsWith(tvDir))
              {
                type = MediaType.tvSeries;
                break;
              }
            }
            for (String vidExcludeDir : vidExcludeDirs)
            {
              if (filePath.startsWith(vidExcludeDir))
              {
                type = null;
                break;
              }
            }
          }
        }
        else if (mediaSettings.getTypeDeterminer() == MediaTypeDeterminer.metadata)
        {
          if (vid.has("Season"))
          {
            String season = vid.getString("Season");
            if (!season.isEmpty() && !season.equals("0"))
              type = MediaType.tvSeries;
          }
          if (type != MediaType.tvSeries && vid.has("Inetref"))
          {
            String inetref = vid.getString("Inetref");
            if (!inetref.isEmpty() && !inetref.equals("00000000"))
              type = MediaType.movies;
          }
        }
        
        if (type == mediaType)
          mediaList.addItemUnderPathCategory(buildVideoItem(type, vid));
      }
    }
    else if (list.has("ProgramList"))
    {
      // recordings
      JSONObject infoList = list.getJSONObject("ProgramList");
      mediaList.setRetrieveDate(parseMythDateTime(infoList.getString("AsOf")));
      mediaList.setCount(infoList.getString("Count"));
      JSONArray recs = infoList.getJSONArray("Programs");
      for (int i = 0; i < recs.length(); i++)
      {
        JSONObject rec = (JSONObject) recs.get(i);
        Item recItem = buildRecordingItem(rec);
        if (sortType == null || sortType == SortType.byTitle)<commit_msg>Fix mythtv services video count.<commit_after>package com.oakesville.mythling.util;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

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

import android.util.Log;

import com.oakesville.mythling.BuildConfig;
import com.oakesville.mythling.app.AppSettings;
import com.oakesville.mythling.media.Category;
import com.oakesville.mythling.media.Item;
import com.oakesville.mythling.media.LiveStreamInfo;
import com.oakesville.mythling.media.MediaList;
import com.oakesville.mythling.media.MediaSettings;
import com.oakesville.mythling.media.Movie;
import com.oakesville.mythling.media.Recording;
import com.oakesville.mythling.media.StorageGroup;
import com.oakesville.mythling.media.TvEpisode;
import com.oakesville.mythling.media.TvShow;
import com.oakesville.mythling.media.Video;
import com.oakesville.mythling.media.MediaSettings.MediaType;
import com.oakesville.mythling.media.MediaSettings.MediaTypeDeterminer;
import com.oakesville.mythling.media.MediaSettings.SortType;

public class MythTvParser implements MediaListParser
{
  private static final String TAG = MythTvParser.class.getSimpleName();

  private String json;
  private AppSettings appSettings;
  
  public MythTvParser(String json, AppSettings appSettings)
  {
    this.json = json;
    this.appSettings = appSettings;
  }
  
  public MediaList parseMediaList(MediaType mediaType) throws JSONException, ParseException
  {
    return parseMediaList(mediaType, null, null);
  }

  public MediaList parseMediaList(MediaType mediaType, StorageGroup storageGroup, String basePath) throws JSONException, ParseException
  {
    MediaList mediaList = new MediaList();
    mediaList.setMediaType(mediaType);
    mediaList.setStorageGroup(storageGroup);
    mediaList.setBasePath(basePath);
    long startTime = System.currentTimeMillis();
    JSONObject list = new JSONObject(json);
    SortType sortType = appSettings.getMediaSettings().getSortType(); 
    if (list.has("VideoMetadataInfoList"))
    {
      // videos, movies, or tvSeries
      MediaSettings mediaSettings = appSettings.getMediaSettings();
      JSONObject infoList = list.getJSONObject("VideoMetadataInfoList");
      mediaList.setRetrieveDate(parseMythDateTime(infoList.getString("AsOf")));
      JSONArray vids = infoList.getJSONArray("VideoMetadataInfos");
      
      String[] movieDirs = appSettings.getMovieDirs();
      String[] tvSeriesDirs = appSettings.getTvSeriesDirs();
      String[] vidExcludeDirs = appSettings.getVidExcludeDirs();

      int count = 0;
      for (int i = 0; i < vids.length(); i++)
      {
        JSONObject vid = (JSONObject) vids.get(i);
        MediaType type = MediaType.videos;
        // determine type
        if (mediaSettings.getTypeDeterminer() == MediaTypeDeterminer.directories)
        {
          if (vid.has("FileName"))
          {
            String filePath = vid.getString("FileName");
            if (mediaList.getStorageGroup() == null && filePath.startsWith(basePath + "/"))
              filePath = filePath.substring(basePath.length() + 1);
            
            for (String movieDir : movieDirs)
            {
              if (filePath.startsWith(movieDir))
              {
                type = MediaType.movies;
                break;
              }
            }
            for (String tvDir : tvSeriesDirs)
            {
              if (filePath.startsWith(tvDir))
              {
                type = MediaType.tvSeries;
                break;
              }
            }
            for (String vidExcludeDir : vidExcludeDirs)
            {
              if (filePath.startsWith(vidExcludeDir))
              {
                type = null;
                break;
              }
            }
          }
        }
        else if (mediaSettings.getTypeDeterminer() == MediaTypeDeterminer.metadata)
        {
          if (vid.has("Season"))
          {
            String season = vid.getString("Season");
            if (!season.isEmpty() && !season.equals("0"))
              type = MediaType.tvSeries;
          }
          if (type != MediaType.tvSeries && vid.has("Inetref"))
          {
            String inetref = vid.getString("Inetref");
            if (!inetref.isEmpty() && !inetref.equals("00000000"))
              type = MediaType.movies;
          }
        }
        
        if (type == mediaType)
        {
          mediaList.addItemUnderPathCategory(buildVideoItem(type, vid));
          count++;
        }
      }
      mediaList.setCount(count);
    }
    else if (list.has("ProgramList"))
    {
      // recordings
      JSONObject infoList = list.getJSONObject("ProgramList");
      mediaList.setRetrieveDate(parseMythDateTime(infoList.getString("AsOf")));
      mediaList.setCount(infoList.getString("Count"));
      JSONArray recs = infoList.getJSONArray("Programs");
      for (int i = 0; i < recs.length(); i++)
      {
        JSONObject rec = (JSONObject) recs.get(i);
        Item recItem = buildRecordingItem(rec);
        if (sortType == null || sortType == SortType.byTitle)
{'fraction_non_alphanumeric': 0.10674211182506953, 'fraction_numerical': 0.0023976215594130623, 'mean_word_length': 2.6666666666666665, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7653897', 'n_tokens_mistral': 3045, 'n_tokens_neox': 2990, 'n_words': 541}