summaryrefslogtreecommitdiffstats
path: root/xbmc/filesystem/VideoDatabaseFile.cpp
blob: 036eeba77d9387ae9c7ef8be7968cd0ac7686020 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "VideoDatabaseFile.h"

#include "URL.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "video/VideoDatabase.h"
#include "video/VideoInfoTag.h"

using namespace XFILE;

CVideoDatabaseFile::CVideoDatabaseFile(void)
  : COverrideFile(true)
{ }

CVideoDatabaseFile::~CVideoDatabaseFile(void) = default;

CVideoInfoTag CVideoDatabaseFile::GetVideoTag(const CURL& url)
{
  CVideoInfoTag tag;

  std::string strFileName = URIUtils::GetFileName(url.Get());
  if (strFileName.empty())
    return tag;

  URIUtils::RemoveExtension(strFileName);
  if (!StringUtils::IsNaturalNumber(strFileName))
    return tag;
  long idDb = atol(strFileName.c_str());

  VideoDbContentType type = GetType(url);
  if (type == VideoDbContentType::UNKNOWN)
    return tag;

  CVideoDatabase videoDatabase;
  if (!videoDatabase.Open())
    return tag;

  tag = videoDatabase.GetDetailsByTypeAndId(type, idDb);

  return tag;
}

VideoDbContentType CVideoDatabaseFile::GetType(const CURL& url)
{
  std::string strPath = URIUtils::GetDirectory(url.Get());
  if (strPath.empty())
    return VideoDbContentType::UNKNOWN;

  std::vector<std::string> pathElem = StringUtils::Split(strPath, "/");
  if (pathElem.size() == 0)
    return VideoDbContentType::UNKNOWN;

  std::string itemType = pathElem.at(2);
  VideoDbContentType type;
  if (itemType == "movies" || itemType == "recentlyaddedmovies")
    type = VideoDbContentType::MOVIES;
  else if (itemType == "episodes" || itemType == "recentlyaddedepisodes" || itemType == "inprogresstvshows" || itemType == "tvshows")
    type = VideoDbContentType::EPISODES;
  else if (itemType == "musicvideos" || itemType == "recentlyaddedmusicvideos")
    type = VideoDbContentType::MUSICVIDEOS;
  else
    type = VideoDbContentType::UNKNOWN;

  return type;
}


std::string CVideoDatabaseFile::TranslatePath(const CURL& url)
{
  std::string strFileName = URIUtils::GetFileName(url.Get());
  if (strFileName.empty())
    return "";

  URIUtils::RemoveExtension(strFileName);
  if (!StringUtils::IsNaturalNumber(strFileName))
    return "";
  long idDb = atol(strFileName.c_str());

  VideoDbContentType type = GetType(url);
  if (type == VideoDbContentType::UNKNOWN)
    return "";

  CVideoDatabase videoDatabase;
  if (!videoDatabase.Open())
    return "";

  std::string realFilename;
  videoDatabase.GetFilePathById(idDb, realFilename, type);

  return realFilename;
}