MythTV  master
mythmetadatalookup.cpp
Go to the documentation of this file.
1 // C headers
2 #include <unistd.h>
3 
4 // C++ headers
5 #include <iostream>
6 #include <memory>
7 
8 // Qt headers
9 #include <QtGlobal>
10 #include <QCoreApplication>
11 #include <QEventLoop>
12 #ifdef Q_OS_DARWIN
13 #include <QProcessEnvironment>
14 #endif
15 
16 // MythTV
17 #include "libmyth/mythcontext.h"
19 #include "libmythbase/exitcodes.h"
20 #include "libmythbase/mythconfig.h"
21 #include "libmythbase/mythdb.h"
25 #include "libmythbase/mythversion.h"
27 #include "libmythtv/jobqueue.h"
28 
29 // MythMetadataLookup
30 #include "lookup.h"
32 
33 namespace
34 {
35  void cleanup()
36  {
37  delete gContext;
38  gContext = nullptr;
40  }
41 }
42 
43 int main(int argc, char *argv[])
44 {
46  if (!cmdline.Parse(argc, argv))
47  {
50  }
51 
52  if (cmdline.toBool("showhelp"))
53  {
55  return GENERIC_EXIT_OK;
56  }
57 
58  if (cmdline.toBool("showversion"))
59  {
61  return GENERIC_EXIT_OK;
62  }
63 
64  QCoreApplication a(argc, argv);
65  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHMETADATALOOKUP);
66 
67 #ifdef Q_OS_DARWIN
68  QString path = QCoreApplication::applicationDirPath();
69  setenv("PYTHONPATH",
70  QString("%1/../Resources/lib/%2:/../Resources/lib/%2/site-packages:/../Resources/lib/%2/lib-dynload:%3")
71  .arg(path)
72  .arg(QFileInfo(PYTHON_EXE).fileName())
73  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
74  .toUtf8().constData(), 1);
75 #endif
76 
77  int retval = cmdline.ConfigureLogging();
78  if (retval != GENERIC_EXIT_OK)
79  return retval;
80 
82  // Don't listen to console input
83  close(0);
84 
85  CleanupGuard callCleanup(cleanup);
86 
87 #ifndef _WIN32
89 #endif
90 
91  gContext = new MythContext(MYTH_BINARY_VERSION);
92  if (!gContext->Init(false))
93  {
94  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
96  }
97 
98  myth_nice(19);
99 
100  MythTranslation::load("mythfrontend");
101 
102  std::unique_ptr<LookerUpper> lookup {new LookerUpper};
103 
104  LOG(VB_GENERAL, LOG_INFO,
105  "Testing grabbers and metadata sites for functionality...");
106  if (!LookerUpper::AllOK())
107  return GENERIC_EXIT_NOT_OK;
108  LOG(VB_GENERAL, LOG_INFO,
109  "All grabbers tested and working. Continuing...");
110 
111  if (cmdline.toBool("jobid"))
112  {
113  uint chanid = 0;
114  QDateTime starttime;
115  int jobType = JOB_METADATA;
116 
117  if (!JobQueue::GetJobInfoFromID(cmdline.toInt("jobid"), jobType,
118  chanid, starttime))
119  {
120  LOG(VB_GENERAL, LOG_INFO,
121  QString("No valid job found for jobid: %1")
122  .arg(cmdline.toInt("jobid")));
123  return GENERIC_EXIT_NOT_OK;
124  }
125 
126  lookup->HandleSingleRecording(chanid, starttime,
127  cmdline.toBool("refresh-rules"));
128  }
129  else if (cmdline.toBool("chanid") && cmdline.toBool("starttime"))
130  {
131  lookup->HandleSingleRecording(cmdline.toUInt("chanid"),
132  cmdline.toDateTime("starttime"),
133  cmdline.toBool("refresh-rules"));
134  }
135  else if (cmdline.toBool("refresh-all-rules"))
136  {
137  lookup->HandleAllRecordingRules();
139  }
140  else if (cmdline.toBool("refresh-all-artwork"))
141  {
142  lookup->HandleAllArtwork(false);
143  }
144  else if (cmdline.toBool("refresh-all-artwork-dangerously"))
145  {
146  lookup->HandleAllArtwork(true);
147  }
148  else
149  {
150  // refresh-all is default behavior if no other arguments given
152  lookup->HandleAllRecordings(cmdline.toBool("refresh-rules"));
153  }
154 
155  while (lookup->StillWorking())
156  {
157  sleep(1);
158  qApp->processEvents();
159  }
160 
161  LOG(VB_GENERAL, LOG_NOTICE, "MythMetadataLookup run complete.");
162 
163  return GENERIC_EXIT_OK;
164 }
mythdb.h
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythcommflag.cpp:72
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
setenv
#define setenv(x, y, z)
Definition: compat.h:89
JobQueue::GetJobInfoFromID
static bool GetJobInfoFromID(int jobID, int &jobType, uint &chanid, QDateTime &recstartts)
Definition: jobqueue.cpp:664
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
LookerUpper
Definition: lookup.h:10
close
#define close
Definition: compat.h:43
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1552
MythMetadataLookupCommandLineParser
Definition: mythmetadatalookup_commandlineparser.h:4
GENERIC_EXIT_INVALID_CMDLINE
@ GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:18
mythlogging.h
JOB_METADATA
@ JOB_METADATA
Definition: jobqueue.h:80
signalhandling.h
GENERIC_EXIT_OK
@ GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:13
CleanupGuard
Definition: cleanupguard.h:6
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1380
mythtranslation.h
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2246
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1396
jobqueue.h
LookerUpper::AllOK
static bool AllOK()
Definition: lookup.h:16
SignalHandler::Init
static void Init(QObject *parent=nullptr)
Definition: signalhandling.cpp:128
mythmiscutil.h
anonymous_namespace{mythmetadatalookup.cpp}::cleanup
void cleanup()
Definition: mythmetadatalookup.cpp:35
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2197
LookerUpper::CopyRuleInetrefsToRecordings
static void CopyRuleInetrefsToRecordings()
Definition: lookup.cpp:199
cleanupguard.h
mythcontext.h
GENERIC_EXIT_NOT_OK
@ GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:14
lookup.h
MythCommandLineParser::ConfigureLogging
int ConfigureLogging(const QString &mask="general", bool progress=false)
Read in logging options and initialize the logging interface.
Definition: mythcommandlineparser.cpp:2870
main
int main(int argc, char *argv[])
Definition: mythmetadatalookup.cpp:43
mythmetadatalookup_commandlineparser.h
myth_nice
bool myth_nice(int val)
Definition: mythmiscutil.cpp:656
MythTranslation::load
static void load(const QString &module_name)
Load a QTranslator for the user's preferred language.
Definition: mythtranslation.cpp:37
exitcodes.h
MythCommandLineParser::toInt
int toInt(const QString &key) const
Returns stored QVariant as an integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2219
MYTH_APPNAME_MYTHMETADATALOOKUP
static constexpr const char * MYTH_APPNAME_MYTHMETADATALOOKUP
Definition: mythcorecontext.h:33
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2453
gContext
MythContext * gContext
This global variable contains the MythContext instance for the application.
Definition: mythcontext.cpp:64
MythContext::Init
bool Init(bool gui=true, bool promptForBackend=false, bool disableAutoDiscovery=false, bool ignoreDB=false)
Definition: mythcontext.cpp:1608
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:135
GENERIC_EXIT_NO_MYTHCONTEXT
@ GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:16
uint
unsigned int uint
Definition: freesurround.h:24