[mythtv] [PATCH] Adding recurring program entries for manually scheduled itens

Mauricio Piacentini mauricio at tabuleiro.com
Sun Oct 10 03:12:39 UTC 2004


Hi. There is stable grabber for Brazil (yet), so my wife and I added 
channel information manually to the database and are scheduling all 
recordings manually for the time being.

However, as you know this really does not work well for recurring shows, 
due to the way the manual recording option was added. The problem is 
that even if you set a recording to repeat daily or weekly at the same 
timeslot the subsequent recordings will not be done, as there is no new 
program data  and the scheduler can not do its magic...

So I patched mythfilldatabase to address our needs, adding a new option:

--add-recurring-manual-recordings

I believe this may be useful for other users in countries where XMLTV 
grabbers or DataDirect are not available, and people are scheduling 
their shows manually as well.

What this option does is simple: it queries the database for existing 
manually scheduled recordings that were set to repeat weekly or daily at 
the same timeslot. It then adds program data for the next 7 days, 
matching the names and slots of shows scheduled for manual recording at 
a daily or weekly timeslot, and adjusting the description/subtitle 
accordingly. It can be used with the --no-delete and --quiet flags.

This option relies on the category of the record to be "Manual 
recording", which is the way we found to identify the entries added 
manually. It produces entries that are exactly the same as the "fake" 
entry added when you use the Manual schedule feature, just automatically 
placed at the correct daily (or weekly) timeslot, ready to be picked by 
the Scheduler.

Let me know if you think this is something that should be added to 
mythfilldatabase, or if I should package it as a standalone utility. The 
patch is not small but really does not change anything in the way 
mythfilldatabase processes data if the option is not used, so I believe 
it is harmless for people not using it.

Best regards,
Mauricio

PS- If there is another (better) way to submit patches please let me 
know, I assumed it is ok to post them to myth-dev.
-------------- next part --------------
--- filldata.0.16.cpp	2004-08-18 23:02:26.000000000 -0300
+++ filldata.cpp	2004-10-09 22:26:32.000000000 -0300
@@ -2833,6 +2833,155 @@
     return count;
 }
 
+
+void AddRecurringManualRecordings(QSqlDatabase *db)
+{
+    QString title, subtitle, description, category, starttime, endtime, seriesid, programid, startdate;
+    int chanid, type;
+
+    QSqlQuery query;
+
+    query.prepare("SELECT chanid, type, starttime, endtime, title, subtitle, description, seriesid, programid, startdate "
+                  "FROM record WHERE (type=:RECORDTYPEDAILYSLOT OR type=:RECORDTYPEWEEKLYSLOT) AND category=:MANUAL");
+    query.bindValue(":RECORDTYPEDAILYSLOT", 2);
+    query.bindValue(":RECORDTYPEWEEKLYSLOT", 5);
+    query.bindValue(":MANUAL", "Manual recording");
+
+    query.exec();
+
+    if (query.isActive() && query.numRowsAffected()) 
+    {
+        while (query.next())
+        {
+            chanid = query.value(0).toInt();
+	    type = query.value(1).toInt();
+            starttime = query.value(2).toString();
+            endtime = query.value(3).toString();
+            title = query.value(4).toString();
+	    subtitle  = query.value(5).toString();
+	    description  = query.value(6).toString();
+	    seriesid = query.value(7).toString();
+	    programid =  query.value(8).toString();
+	    startdate =  query.value(9).toString();
+
+	    if (!quiet)
+            	cout << "Manual recording schedule recurring program found at channel " 
+                 << chanid << " - " << title
+                 << " from "
+		 << starttime
+                 << " to "
+	         << endtime<< endl;
+		 		 
+	   QDate today = QDate::currentDate();
+	   QDate thisday = today;
+	   //simulate getting programs for a week
+	   for (int i=0; i<7; i++)
+	   {
+		int hour, min, sec;
+		bool addtoguide = true;
+		hour = atoi(starttime.mid(0, 2).ascii());
+		min = atoi(starttime.mid(3, 2).ascii());
+		sec = atoi(starttime.mid(6, 2).ascii());
+		QTime starttimet = QTime(hour, min, sec);
+		QDateTime recstartdate = QDateTime(thisday, starttimet);
+		hour = atoi(endtime.mid(0, 2).ascii());
+		min = atoi(endtime.mid(3, 2).ascii());
+		sec = atoi(endtime.mid(6, 2).ascii());
+		QTime endtimet = QTime(hour, min, sec);
+		QDateTime recenddate = QDateTime(thisday, endtimet);
+		if (endtimet<starttimet)
+		{
+			//program ends after midnight
+			recenddate = recenddate.addDays(1);
+		}
+		
+		if (type==5) //check weekly schedule against current date
+		{
+			int year, month, day;
+			year = atoi(startdate.mid(0, 4).ascii());
+			month = atoi(startdate.mid(5, 2).ascii());
+			day = atoi(startdate.mid(8, 2).ascii());
+			QDate initialschedule = QDate(year, month, day);
+			if (initialschedule.dayOfWeek()==recstartdate.date().dayOfWeek())
+				addtoguide = true;
+			else
+				addtoguide = false;
+		} 
+		
+		if ( recstartdate < QDateTime::currentDateTime ())
+		{//don't add programs that have already passed
+			addtoguide = false;
+		}
+		
+		if (addtoguide==true)
+		{		
+			QString startstr = recstartdate.toString("yyyyMMddhhmmss");
+            		QString endstr = recenddate.toString("yyyyMMddhhmmss");
+				
+			if (no_delete==false)
+			{
+				QSqlQuery delquery;
+				delquery.prepare("DELETE FROM program WHERE "
+					"chanid=:CHANID AND starttime>=:START "
+					"AND starttime<:END;");
+				delquery.bindValue(":CHANID", chanid);
+				delquery.bindValue(":START", startstr);
+				delquery.bindValue(":END", endstr);
+				delquery.exec();
+				cout << "deleting" << endl;
+			 }
+			
+			QSqlQuery insertquery;	
+			insertquery.prepare("INSERT INTO program (chanid,starttime,endtime,"
+					"title,title_pronounce,subtitle,description,category,"
+					"category_type,airdate,stars,previouslyshown) "
+					"VALUES(:CHANID,:STARTTIME,:ENDTIME,:TITLE,"
+					":TITLE_PRONOUNCE,:SUBTITLE,"
+					":DESCRIPTION,:CATEGORY,:CATEGORY_TYPE,:AIRDATE,"
+					":STARS,:PREVIOUSLYSHOWN);");
+			insertquery.bindValue(":CHANID", chanid);
+			insertquery.bindValue(":STARTTIME", startstr);
+			insertquery.bindValue(":ENDTIME", endstr);
+			insertquery.bindValue(":TITLE", title);
+			insertquery.bindValue(":TITLE_PRONOUNCE", "");
+			QString recsubtitle = recstartdate.toString("yyyy/MM/dd") + " " +
+			recstartdate.toString("h:mm AP");
+			insertquery.bindValue(":SUBTITLE", recsubtitle);
+			int recduration = recstartdate.secsTo(recenddate) / 60;
+			QString recdescription = recstartdate.toString("ddd MMMM d")+ QString(" - (%1min) - Manual recording").arg(recduration);
+			insertquery.bindValue(":DESCRIPTION", recdescription);
+			insertquery.bindValue(":CATEGORY", "Manual recording");
+			insertquery.bindValue(":CATEGORY_TYPE", "");
+			insertquery.bindValue(":AIRDATE", "");
+			insertquery.bindValue(":STARS", 0);
+			insertquery.bindValue(":PREVIOUSLYSHOWN", 0);
+			insertquery.bindValue(":SERIESID", seriesid);
+			insertquery.bindValue(":PROGRAMID", programid);
+			
+			if (!insertquery.exec())
+			{
+				if (!quiet)
+					cout << "Error inserting program" << endl;
+			} else {
+				if (!quiet)
+					cout << "Added new show time for " 
+					<< title
+					<< " from "
+					<< recstartdate.toString("yyyy/MM/dd hh:mm") 
+					<< " to "
+					<< recenddate.toString("yyyy/MM/dd hh:mm") << endl;
+			}
+		}
+		
+		//increment day
+		thisday = thisday.addDays(1);
+	  }
+		 
+	}//end program query
+	ScheduledRecording::signalChange(db);
+    }
+}
+
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv, false);
@@ -2854,6 +3003,8 @@
     QString export_icon_map_filename("iconmap.xml");
 
     bool update_icon_map = false;
+    
+    bool add_recurring_manual_recordings = false;
 
     while (argpos < a.argc())
     {
@@ -3030,6 +3181,11 @@
                 }
             }
         }
+	else if (!strcmp(a.argv()[argpos], "--add-recurring-manual-recordings"))
+        {
+            add_recurring_manual_recordings = true;
+	    grab_data = false;
+        }
         else if (!strcmp(a.argv()[argpos], "-h") ||
                  !strcmp(a.argv()[argpos], "--help"))
         {
@@ -3088,6 +3244,8 @@
             cout << "   Resets your icon map (pass all to reset channel icons as well)\n";
             cout << "--mark-repeats\n";
             cout << "   Marks any programs with a OriginalAirDate earlier than their start date as a repeat\n";
+	    cout << "--add-recurring-manual-recordings\n";
+	    cout << "   For users in countries with no grabbers available. This option adds program data for the next 7 days, matching the names and slots of shows scheduled for manual recording at a daily or weekly timeslot. \n";
             cout << "\n";
 #if 0
             cout << "--dd-grab-all\n";
@@ -3119,12 +3277,54 @@
         cerr << "couldn't open db\n";
         return -1;
     }
-
+    
     gContext->LogEntry("mythfilldatabase", LP_INFO,
                        "Listings Download Started", "");
     
     QSqlQuery query;
     
+    if (add_recurring_manual_recordings)
+    {
+    	//need to be done before db is opened
+	//basically initialize and end in the same way as the (from_file)
+	// code path below.
+	QString status = "currently running.";
+        QDateTime GuideDataBefore, GuideDataAfter;
+
+        query.exec(QString("UPDATE settings SET data ='%1' "
+                           "WHERE value='mythfilldatabaseLastRunStart'")
+                           .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")));
+
+        query.exec(QString("UPDATE settings SET data ='%1' "
+                           "WHERE value='mythfilldatabaseLastRunStatus'")
+                           .arg(status));
+
+        query.exec(QString("SELECT MAX(endtime) FROM program;"));
+        if (query.isActive() && query.numRowsAffected())
+        {
+            query.next();
+
+            if (!query.isNull(0))
+                GuideDataBefore = QDateTime::fromString(query.value(0).toString(),
+                                                    Qt::ISODate);
+        }
+	
+    	AddRecurringManualRecordings(db);
+        clearOldDBEntries();
+
+        query.exec(QString("UPDATE settings SET data ='%1' "
+                           "WHERE value='mythfilldatabaseLastRunEnd'")
+                          .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm")));
+
+        status = "Recurring manual recordings updated.";
+
+        query.exec(QString("UPDATE settings SET data ='%1' "
+                           "WHERE value='mythfilldatabaseLastRunStatus'")
+                           .arg(status));
+    }
+
+    
+    
     if (!grab_data)
     {
     }
@@ -3317,3 +3517,5 @@
 
     return 0;
 }
+
+


More information about the mythtv-dev mailing list