MythTV  master
mythtv/programs/mythmetadatalookup/main.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 // libmyth headers
17 #include "exitcodes.h"
18 #include "mythcontext.h"
19 #include "mythdb.h"
20 #include "mythversion.h"
21 #include "mythmiscutil.h"
22 #include "jobqueue.h"
23 #include "mythtranslation.h"
24 #include "mythconfig.h"
25 #include "commandlineparser.h"
26 #include "loggingserver.h"
27 #include "mythlogging.h"
28 #include "signalhandling.h"
29 #include "cleanupguard.h"
30 
31 #include "lookup.h"
32 
33 namespace
34 {
35  void cleanup()
36  {
37  delete gContext;
38  gContext = nullptr;
40  }
41 }
42 
43 bool inJobQueue = false;
44 
45 int main(int argc, char *argv[])
46 {
48  if (!cmdline.Parse(argc, argv))
49  {
52  }
53 
54  if (cmdline.toBool("showhelp"))
55  {
57  return GENERIC_EXIT_OK;
58  }
59 
60  if (cmdline.toBool("showversion"))
61  {
63  return GENERIC_EXIT_OK;
64  }
65 
66  QCoreApplication a(argc, argv);
67  QCoreApplication::setApplicationName(MYTH_APPNAME_MYTHMETADATALOOKUP);
68 
69 #ifdef Q_OS_DARWIN
70  QString path = QCoreApplication::applicationDirPath();
71  setenv("PYTHONPATH",
72  QString("%1/../Resources/lib/%2/site-packages:%3")
73  .arg(path)
74  .arg(QFileInfo(PYTHON_EXE).fileName())
75  .arg(QProcessEnvironment::systemEnvironment().value("PYTHONPATH"))
76  .toUtf8().constData(), 1);
77 #endif
78 
79  int retval = cmdline.ConfigureLogging();
80  if (retval != GENERIC_EXIT_OK)
81  return retval;
82 
84  // Don't listen to console input
85  close(0);
86 
87  CleanupGuard callCleanup(cleanup);
88 
89 #ifndef _WIN32
90  QList<int> signallist;
91  signallist << SIGINT << SIGTERM << SIGSEGV << SIGABRT << SIGBUS << SIGFPE
92  << SIGILL;
93 #ifndef Q_OS_DARWIN
94  signallist << SIGRTMIN;
95 #endif
96  SignalHandler::Init(signallist);
98 #endif
99 
101  if (!gContext->Init(false))
102  {
103  LOG(VB_GENERAL, LOG_ERR, "Failed to init MythContext, exiting.");
105  }
106 
107  myth_nice(19);
108 
109  MythTranslation::load("mythfrontend");
110 
111  std::unique_ptr<LookerUpper> lookup {new LookerUpper};
112 
113  LOG(VB_GENERAL, LOG_INFO,
114  "Testing grabbers and metadata sites for functionality...");
115  if (!LookerUpper::AllOK())
116  return GENERIC_EXIT_NOT_OK;
117  LOG(VB_GENERAL, LOG_INFO,
118  "All grabbers tested and working. Continuing...");
119 
120  if (cmdline.toBool("jobid"))
121  {
122  uint chanid = 0;
123  QDateTime starttime;
124  int jobType = JOB_METADATA;
125 
126  if (!JobQueue::GetJobInfoFromID(cmdline.toInt("jobid"), jobType,
127  chanid, starttime))
128  {
129  LOG(VB_GENERAL, LOG_INFO,
130  QString("No valid job found for jobid: %1")
131  .arg(cmdline.toInt("jobid")));
132  return GENERIC_EXIT_NOT_OK;
133  }
134 
135  lookup->HandleSingleRecording(chanid, starttime,
136  cmdline.toBool("refresh-rules"));
137  }
138  else if (cmdline.toBool("chanid") && cmdline.toBool("starttime"))
139  {
140  lookup->HandleSingleRecording(cmdline.toUInt("chanid"),
141  cmdline.toDateTime("starttime"),
142  cmdline.toBool("refresh-rules"));
143  }
144  else if (cmdline.toBool("refresh-all-rules"))
145  {
146  lookup->HandleAllRecordingRules();
148  }
149  else if (cmdline.toBool("refresh-all-artwork"))
150  lookup->HandleAllArtwork(false);
151  else if (cmdline.toBool("refresh-all-artwork-dangerously"))
152  lookup->HandleAllArtwork(true);
153  else
154  {
155  // refresh-all is default behavior if no other arguments given
157  lookup->HandleAllRecordings(cmdline.toBool("refresh-rules"));
158  }
159 
160  while (lookup->StillWorking())
161  {
162  sleep(1);
163  qApp->processEvents();
164  }
165 
166  LOG(VB_GENERAL, LOG_NOTICE, "MythMetadataLookup run complete.");
167 
168  return GENERIC_EXIT_OK;
169 }
GENERIC_EXIT_OK
#define GENERIC_EXIT_OK
Exited with no error.
Definition: exitcodes.h:10
inJobQueue
bool inJobQueue
Definition: mythtv/programs/mythmetadatalookup/main.cpp:43
mythdb.h
MythContext
Startup context for MythTV.
Definition: mythcontext.h:43
setenv
#define setenv(x, y, z)
Definition: compat.h:164
JobQueue::GetJobInfoFromID
static bool GetJobInfoFromID(int jobID, int &jobType, uint &chanid, QDateTime &recstartts)
Definition: jobqueue.cpp:674
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
GENERIC_EXIT_INVALID_CMDLINE
#define GENERIC_EXIT_INVALID_CMDLINE
Command line parse error.
Definition: exitcodes.h:15
LookerUpper
Definition: lookup.h:10
close
#define close
Definition: compat.h:17
mythversion.h
MythCommandLineParser::Parse
virtual bool Parse(int argc, const char *const *argv)
Loop through argv and populate arguments with values.
Definition: mythcommandlineparser.cpp:1449
MythMetadataLookupCommandLineParser
Definition: mythmetadatalookup/commandlineparser.h:4
mythlogging.h
main
int main(int argc, char *argv[])
Definition: mythtv/programs/mythmetadatalookup/main.cpp:45
signalhandling.h
CleanupGuard
Definition: cleanupguard.h:6
SIGHUP
#define SIGHUP
Definition: compat.h:216
MythCommandLineParser::PrintVersion
static void PrintVersion(void)
Print application version information.
Definition: mythcommandlineparser.cpp:1277
mythtranslation.h
MYTH_APPNAME_MYTHMETADATALOOKUP
#define MYTH_APPNAME_MYTHMETADATALOOKUP
Definition: mythcorecontext.h:35
MythCommandLineParser::toUInt
uint toUInt(const QString &key) const
Returns stored QVariant as an unsigned integer, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2144
MythCommandLineParser::PrintHelp
void PrintHelp(void) const
Print command line option help.
Definition: mythcommandlineparser.cpp:1293
jobqueue.h
uint
unsigned int uint
Definition: compat.h:144
LookerUpper::AllOK
static bool AllOK()
Definition: lookup.h:16
commandlineparser.h
cleanup
static QString cleanup(const QString &str)
Definition: remoteencoder.cpp:672
MYTH_BINARY_VERSION
#define MYTH_BINARY_VERSION
Update this whenever the plug-in ABI changes.
Definition: mythversion.h:15
cmdline
MythCommFlagCommandLineParser cmdline
Definition: mythtv/programs/mythcommflag/main.cpp:75
mythmiscutil.h
GENERIC_EXIT_NOT_OK
#define GENERIC_EXIT_NOT_OK
Exited with error.
Definition: exitcodes.h:11
MythCommandLineParser::toBool
bool toBool(const QString &key) const
Returns stored QVariant as a boolean.
Definition: mythcommandlineparser.cpp:2095
LookerUpper::CopyRuleInetrefsToRecordings
static void CopyRuleInetrefsToRecordings()
Definition: lookup.cpp:193
cleanupguard.h
JOB_METADATA
@ JOB_METADATA
Definition: jobqueue.h:78
mythcontext.h
GENERIC_EXIT_NO_MYTHCONTEXT
#define GENERIC_EXIT_NO_MYTHCONTEXT
No MythContext available.
Definition: exitcodes.h:13
SignalHandler::SetHandler
static void SetHandler(int signum, SigHandlerFunc handler)
Definition: signalhandling.cpp:141
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:2756
myth_nice
bool myth_nice(int val)
Definition: mythmiscutil.cpp:707
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:2117
loggingserver.h
MythCommandLineParser::toDateTime
QDateTime toDateTime(const QString &key) const
Returns stored QVariant as a QDateTime, falling to default if not provided.
Definition: mythcommandlineparser.cpp:2349
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:1571
SignalHandler::Done
static void Done(void)
Definition: signalhandling.cpp:134
SignalHandler::Init
static void Init(QList< int > &signallist, QObject *parent=nullptr)
Definition: signalhandling.cpp:127