void SearchTimersResponder::replySearch(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
  QueryHandler q("/searchtimers/search", request);
  vdrlive::SearchResults searchResults;
  int id = q.getParamAsInt(0);

  EventList* eventList;

  if ( q.isFormat(".json") ) {
     reply.addHeader("Content-Type", "application/json; charset=utf-8");
     eventList = (EventList*)new JsonEventList(&out);
  } else if ( q.isFormat(".html") ) {
     reply.addHeader("Content-Type", "text/html; charset=utf-8");
     eventList = (EventList*)new HtmlEventList(&out);
  } else if ( q.isFormat(".xml") ) {
     reply.addHeader("Content-Type", "text/xml; charset=utf-8");
     eventList = (EventList*)new XmlEventList(&out);
  } else {
     reply.httpReturn(403, "Resources are not available for the selected format. (Use: .json, .xml or .html)");
     return;
  }
  
  searchResults.GetByID(id);

  int start_filter = q.getOptionAsInt("start");
  int limit_filter = q.getOptionAsInt("limit");
  if ( start_filter >= 0 && limit_filter >= 1 )
     eventList->activateLimit(start_filter, limit_filter);
  
  eventList->init();
  int total = 0;
  
  for (vdrlive::SearchResults::iterator item = searchResults.begin(); item != searchResults.end(); ++item) {
    eventList->addEvent((cEvent*)item->GetEvent());
    total++;
  }

  eventList->setTotal(total);
  eventList->finish();
  delete eventList;
}
Esempio n. 2
0
void EventsResponder::replyEvents(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{
  QueryHandler q("/events", request);

  if ( request.method() != "GET") {

     reply.httpReturn(403, "To retrieve information use the GET method!");
     return;
  }


  EventList* eventList;

  if ( q.isFormat(".json") ) {
     reply.addHeader("Content-Type", "application/json; charset=utf-8");
     eventList = (EventList*)new JsonEventList(&out);
  } else if ( q.isFormat(".html") ) {
     reply.addHeader("Content-Type", "text/html; charset=utf-8");
     eventList = (EventList*)new HtmlEventList(&out);
  } else if ( q.isFormat(".xml") ) {
     reply.addHeader("Content-Type", "text/xml; charset=utf-8");
     eventList = (EventList*)new XmlEventList(&out);
  } else {
     reply.httpReturn(403, "Resources are not available for the selected format. (Use: .json or .html)");
     return;
  }

  string channel_id = q.getParamAsString(0);
  int timespan = q.getOptionAsInt("timespan");//q.getParamAsInt(1);
  int from = q.getOptionAsInt("from");//q.getParamAsInt(2);

  int start_filter = q.getOptionAsInt("start");
  int limit_filter = q.getOptionAsInt("limit");
  
  int event_id = q.getParamAsInt(1);//q.getOptionAsInt("eventid");

  string onlyCount = q.getOptionAsString("only_count");

#if APIVERSNUM > 20300
    LOCK_CHANNELS_READ;
    const cChannels& channels = *Channels;
#else
    cChannels& channels = Channels;
#endif

  const cChannel* channel = VdrExtension::getChannel(channel_id);
  if ( channel == NULL ) { 
     /*reply.addHeader("Content-Type", "application/octet-stream");
     string error_message = (string)"Could not find channel with id: " + channel_id + (string)"!";
     reply.httpReturn(404, error_message); 
     return;*/
  }

  int channel_limit = q.getOptionAsInt("chevents");
  if ( channel_limit <= -1 ) channel_limit = 0; // default channel events is 0 -> all
  
  int channel_from = q.getOptionAsInt("chfrom");
  if ( channel_from <= -1 || channel != NULL ) channel_from = 0; // default channel number is 0
  
  int channel_to = q.getOptionAsInt("chto");
  if ( channel_to <= 0 || channel != NULL ) channel_to = channels.Count();
 
  if ( from <= -1 ) from = time(NULL); // default time is now
  if ( timespan <= -1 ) timespan = 0; // default timespan is 0, which means all entries will be returned
  int to = from + timespan;

#if APIVERSNUM > 20300
	LOCK_SCHEDULES_READ;
#else
	cSchedulesLock MutexLock;
	const cSchedules *Schedules = cSchedules::Schedules(MutexLock);
#endif

  if( !Schedules ) {
     reply.httpReturn(404, "Could not find schedules!");
     return;
  }


  if ( start_filter >= 0 && limit_filter >= 1 ) {
     eventList->activateLimit(start_filter, limit_filter);
  }

  bool initialized = false;
  int total = 0;
  for(int i=0; i<channels.Count(); i++) {
     const cSchedule *Schedule = Schedules->GetSchedule(channels.Get(i)->GetChannelID());
     
     if ((channel == NULL || strcmp(channel->GetChannelID().ToString(), channels.Get(i)->GetChannelID().ToString()) == 0) && (i >= channel_from && i <= channel_to)) {
        if (!Schedule) {
           if (channel != NULL) {
              reply.httpReturn(404, "Could not find schedule!");
              return;
           }
        } else {
           if (!initialized) {
              eventList->init();
              initialized = true;
           }

           int old = 0;
           int channel_events = 0;
           for(const cEvent* event = Schedule->Events()->First(); event; event = Schedule->Events()->Next(event)) {
              int ts = event->StartTime();
              int te = ts + event->Duration();
              if ((ts <= to && te > from) || (te > from && timespan == 0)) {
                 if (channel_limit == 0 || channel_limit > channel_events) {
                    if ((event_id < 0 || event_id == (int)event->EventID()) && onlyCount != "true") {
                       eventList->addEvent(event);
                       channel_events++;
                    }
                 }
              } else {
                 if (ts > to) break;
                 if (te <= from) old++;
              }
           }
           total += (Schedule->Events()->Count() - old);
        }
     }
  }
  eventList->setTotal(total);
  eventList->finish();
  delete eventList;
}
Esempio n. 3
0
void EventsResponder::replySearchResult(ostream& out, cxxtools::http::Request& request, cxxtools::http::Reply& reply)
{

  QueryHandler q("/events/search", request);

  if ( request.method() != "POST") {
     reply.httpReturn(403, "To search for information use the POST method!");
     return;
  }

  StreamExtension se(&out);

  string query = q.getBodyAsString("query");
  string search = q.getBodyAsString("search");

  if ( query.length() == 0 && search.length() == 0 ) {
     reply.httpReturn(402, "Query required");
     return;
  }

  EventList* eventList;

  if ( q.isFormat(".json") ) {
     reply.addHeader("Content-Type", "application/json; charset=utf-8");
     eventList = (EventList*)new JsonEventList(&out);
  } else if ( q.isFormat(".html") ) {
     reply.addHeader("Content-Type", "text/html; charset=utf-8");
     eventList = (EventList*)new HtmlEventList(&out);
  } else if ( q.isFormat(".xml") ) {
     reply.addHeader("Content-Type", "text/xml; charset=utf-8");
     eventList = (EventList*)new XmlEventList(&out);
  } else {
     reply.httpReturn(403, "Resources are not available for the selected format. (Use: .json or .html)");
     return;
  }
  eventList->init();
  int start_filter = q.getOptionAsInt("start");
  int limit_filter = q.getOptionAsInt("limit");
  int date_filter = q.getOptionAsInt("date_limit");
  if ( start_filter >= 0 && limit_filter >= 1 ) {
     eventList->activateLimit(start_filter, limit_filter);
  }
  if ( date_filter >= 0  ) {
     eventList->activateDateLimit(date_filter);
  }
  
  int total = 0;
  if ( search.length() > 0 ) {

      vdrlive::SearchTimer* searchtimer = new vdrlive::SearchTimer;
      searchtimer->SetId(0);
      string result = searchtimer->LoadCommonFromQuery(q);

      if (result.length() > 0) {
           reply.httpReturn(406, result.c_str());
           return;
      }

      string query = searchtimer->ToText();
      vdrlive::SearchResults* results = new vdrlive::SearchResults;
      results->GetByQuery(query);

      for (vdrlive::SearchResults::iterator result = results->begin(); result != results->end(); ++result) {

          eventList->addEvent(((cEvent*)result->GetEvent()));
          total++;
      }
      delete searchtimer;
      delete results;

  } else {

      int mode = q.getBodyAsInt("mode");// search mode (0=phrase, 1=and, 2=or, 3=exact, 4=regular expression, 5=fuzzy)
      string channelid = q.getBodyAsString("channelid"); //id !!
      bool use_title = q.getBodyAsString("use_title") == "true";
      bool use_subtitle = q.getBodyAsString("use_subtitle") == "true";
      bool use_description = q.getBodyAsString("use_description") == "true";

      int channel = 0;
      const cChannel* channelInstance = VdrExtension::getChannel(channelid);
      if (channelInstance != NULL) {
         channel = channelInstance->Number();
      }

#if APIVERSNUM > 20300
    LOCK_CHANNELS_READ;
    const cChannels& channels = *Channels;
#else
    cChannels& channels = Channels;
#endif

      if (!use_title && !use_subtitle && !use_description)
         use_title = true;
      if (mode < 0 || mode > 5)
         mode = 0;
      if (channel < 0 || channel > channels.Count())
         channel = 0;
      if (query.length() > 100)
         query = query.substr(0,100); //don't allow more than 100 characters, NOTE: maybe I should add a limitation to the Responderclass?

      struct Epgsearch_searchresults_v1_0* epgquery = new struct Epgsearch_searchresults_v1_0;
      epgquery->query = (char*)query.c_str();
      epgquery->mode = mode;
      epgquery->channelNr = channel;
      epgquery->useTitle = use_title;
      epgquery->useSubTitle = use_subtitle;
      epgquery->useDescription = use_description;

      cPlugin *Plugin = cPluginManager::GetPlugin("epgsearch");
      if (Plugin) {
         if (Plugin->Service("Epgsearch-searchresults-v1.0", NULL)) {
            if (Plugin->Service("Epgsearch-searchresults-v1.0", epgquery)) {
               cList< Epgsearch_searchresults_v1_0::cServiceSearchResult>* result = epgquery->pResultList;
               Epgsearch_searchresults_v1_0::cServiceSearchResult* item = NULL;
               if (result != NULL) {
                  for(int i=0;i<result->Count();i++) {
                     item = result->Get(i);
                     eventList->addEvent(((cEvent*)item->event));
                     total++;
                  }
               }
            } else {
               reply.httpReturn(406, "Internal (epgsearch) error, check parameters.");
            }
         } else {
            reply.httpReturn(405, "Plugin-service not available.");
         }
      } else {
         reply.httpReturn(404, "Plugin not installed!");
      }
      delete epgquery;

  }
  eventList->setTotal(total);
  eventList->finish();
  delete eventList;
}