Пример #1
0
void Query::run(sc::SearchReplyProxy const& reply) {
    try {
        // Start by getting information about the query
        const sc::CannedQuery &query(sc::SearchQueryBase::query());

        // A string to store the location name for the openweathermap query
        std::string ll;
        std::string lol;

        // Access search metadata
        auto metadata = search_metadata();

        // Check for location data
        if(metadata.has_location()) {
            auto location = metadata.location();

            // Check for city and country
            if(location.latitude() && location.longitude()) {

                // Create the "city country" string
                //auto lat = location.latitude();
                //auto lng = location.longitude();

                //ll = location.country_name();
                //ll = std::to_string(lat) + "," + std::to_string(lng);

                // Must be edit
                auto lat = location.latitude();
                auto lng = location.longitude();

                // Buffers for future strings
                char buflat[7];
                char buflng[7];

                // Keep two decimal points
                sprintf(buflat, "%.2f", lat);
                sprintf(buflng, "%.2f", lng);

                // Convert to std::string
                std::string latstr(buflat);
                std::string lngstr(buflng);

                string rlatstr = latstr.replace(2, 1, ".");
                string rlngstr = lngstr.replace(2, 1, ".");
                ll = rlatstr + "," + rlngstr;
            }
        }

        // Fallback to a hardcoded location
        if(ll.empty()) {
            ll = "40.37,49.84";
        }

        // Trim the query string of whitespace
        string query_string = alg::trim_copy(query.query_string());

        Client::TrackRes trackslist;
        if (query_string.empty()) {
            // If the string is empty, provide a specific one
            trackslist = client_.tracks("", ll);
        } else {
            // otherwise, use the query string
            trackslist = client_.tracks(query_string, ll);
        }


        // Register a category for tracks
        auto tracks_cat = reply->register_category("tracks", "", "",
            sc::CategoryRenderer(VENUES_TEMPLATE));
        // register_category(arbitrary category id, header title, header icon, template)
        // In this case, since this is the only category used by our scope,
        // it doesn’t need to display a header title, we leave it as a blank string.


        for (const auto &track : trackslist.tracks) {

            // Use the tracks category
            sc::CategorisedResult res(tracks_cat);

            // We must have a URI
            res.set_uri(track.uri);

            // Our result also needs a track title
            res.set_title(track.title);

            // Set the rest of the attributes, art, artist, etc.
            res.set_art(track.category_icon);
            res["category"] = track.category_name;
            res["address"] = track.address;
            res["venue_photo"] = track.venue_photo;


            // Push the result
            if (!reply->push(res)) {
                // If we fail to push, it means the query has been cancelled.
                return;
            }
        }

    } catch (domain_error &e) {
        // Handle exceptions being thrown by the client API
        cerr << e.what() << endl;
        reply->error(current_exception());

    }
}
Пример #2
0
void Query::run(sc::SearchReplyProxy const& reply)
{
    try {
        // Start by getting information about the query
        const sc::CannedQuery &query(sc::SearchQueryBase::query());

        static ScopeImageCache* icache = nullptr;
        if (!icache)
            icache = new ScopeImageCache(m_config->cache_dir);

        // Get query string and selected department.
        string queryString = query.query_string();
        qCDebug(Qry) << "Query string is:" << QString::fromStdString(queryString);
        string selectedDepartment = query.department_id();
        //qCDebug(Qry) << "Selected department:" << selectedDepartment;

        // Getting source list.
        vector<BaseClient*> sources = enabledSources();
        qCDebug(Qry) << "Source count:" << sources.size();

        // Create and register departments.
        sc::Department::SPtr allDepts = sc::Department::create("", query, _("All"));
        sc::DepartmentList depList;
        vector<Department> list = DepartmentManager::departments();
        for (size_t i = 0; i < list.size(); i++)
            depList.push_back(sc::Department::create(list[i].id, query, list[i].label));
        allDepts->set_subdepartments(depList);
        reply->register_departments(allDepts);

        const int maxCacheUsagePerSouce = settings().at("imageCaching").get_bool() && queryString.empty() ? 8 : 0;
        //qCDebug(Qry) << "maxCacheUsagePerSouce:" << maxCacheUsagePerSouce;

        set<string> uniqueSet;
        for(size_t i = 0; i < sources.size(); ++i)
        {
            string sourceCatName = sources[i]->name();
            auto courseList = sources[i]->courses(queryString);

            auto templ = settings().at("textPosition").get_int() ? CURRENT_TEMPLATE_OV : CURRENT_TEMPLATE;
            auto sourceCategory = reply->register_category(sourceCatName, sourceCatName, "", sc::CategoryRenderer(templ));

            //qCDebug(Qry) << "Processing source:" << sourceCatName;
            int cacheMisses = 0;

            for (const auto &course : courseList)
            {
                // Department check.
                if (!selectedDepartment.empty() && !DepartmentManager::isMatch(course, selectedDepartment))
                    continue;

                // Duplicate results cause Dash to crash.
                if (uniqueSet.count(course.link))
                    continue;
                uniqueSet.insert(course.link);

                string art = course.art;
                string cachedArt = icache->getCached(course.art, cacheMisses < maxCacheUsagePerSouce);
                if (!art.empty() && cachedArt.empty())
                    cacheMisses++;
                else art = cachedArt;

                sc::CategorisedResult res(sourceCategory);
                res.set_uri(course.link);
                res.set_title(course.title);
                res.set_art(art);
                res["headline"] = course.headline;
                res["description"] = course.description;
                res["source"] = sourceCatName;
                res["extra"] = course.extra;
                if (!course.video.empty())
                    res["video_url"] = course.video;
                res["departments"] = vec_to_va(course.departments);

                // Add instructors to map.
                if (course.instructors.size() > 0)
                {
                    std::vector<sc::Variant> images, names, bios;
                    for (auto j = course.instructors.begin(); j != course.instructors.end(); ++j)
                    {
                        images.push_back(sc::Variant(j->image));
                        names.push_back(sc::Variant(j->name));
                        bios.push_back(sc::Variant((j->bio.length() < 150 ? j->bio : j->bio.substr(0, 150) + "...")));
                    }
                    res["instr_images"] = images;
                    res["instr_names"] = names;
                    res["instr_bios"] = bios;
                }

                // Push the result
                if (!reply->push(res))
                    return;
            }

            qCDebug(Qry) << "Finished with source:" << QString::fromStdString(sourceCatName);
        }

    } catch (domain_error &e) {
        qCDebug(Qry) << "Exception:" << QString::fromStdString(e.what());
        reply->error(current_exception());
    }
}
Пример #3
0
void Query::run(sc::SearchReplyProxy const& reply)
{
    try {
        // Start by getting information about the query
        const sc::CannedQuery &query(sc::SearchQueryBase::query());

        // Get the query string
        string query_string = query.query_string();
        qCDebug(Qry) << "Query string is:" << QString::fromStdString(query_string);

        // Checking out which sources are enabled.
        QList<BaseClient*> sources;
        if (settings().at("coursera").get_bool())
            sources.append(&m_coursera);
        if (settings().at("udemy").get_bool())
            sources.append(&m_udemy);
        if (settings().at("edx").get_bool())
            sources.append(&m_edx);
        if (settings().at("udacity").get_bool())
            sources.append(&m_udacity);
        if (settings().at("iversity").get_bool())
            sources.append(&m_iversity);
        if (settings().at("openLearning").get_bool())
            sources.append(&m_openLearning);
        qCDebug(Qry) << "Source count:" << sources.count();

        // Working with departments.
        sc::Department::SPtr all_depts = sc::Department::create("", query, _("All"));
        sc::DepartmentList depList;
        QList<Department> list = DepartmentManager::departments();
        for (int i = 0; i < list.length(); i++)
            depList.push_back(sc::Department::create(list[i].id.toStdString(), query, list[i].label.toStdString()));
        all_depts->set_subdepartments(depList);

        // Register the root department on the reply
        reply->register_departments(all_depts);

        QString selectedDepartment = QString::fromStdString(query.department_id());
        qCDebug(Qry) << "Selected department:" << selectedDepartment;

        QSet<QString> uniqueSet;
        for(int i = 0; i < sources.count(); ++i)
        {
            QString sourceCatName = sources[i]->name();
            auto sourceList = sources[i]->courses(QString::fromStdString(query_string));

            auto templ = settings().at("textPosition").get_int() ? CURRENT_TEMPLATE_OV : CURRENT_TEMPLATE;
            auto sourceCategory = reply->register_category(sourceCatName.toLower().toStdString(),
                                                             sourceCatName.toStdString(),
                                                             "", sc::CategoryRenderer(templ));

            //qCDebug(Qry) << "Processing source:" << sourceCatName;

            for (const auto &course : sourceList)
            {

                // Department check.
                if (!selectedDepartment.isEmpty() && !DepartmentManager::isMatch(course, selectedDepartment))
                    continue;

                // Duplicate results cause Dash to crash.
                if (uniqueSet.contains(course.link))
                {
                    qCDebug(Qry) << "Duplicate:" << course.link;
                    continue;
                }
                uniqueSet.insert(course.link);


                sc::CategorisedResult res(sourceCategory);

                // We must have a URI
                res.set_uri(course.link.toStdString());
                res.set_title(course.title.toStdString());
                res.set_art(course.art.toStdString());
                res["headline"] = course.headline.toStdString();
                res["description"] = course.description.toStdString();
                res["source"] = sourceCatName.toStdString();
                res["extra"] = course.extra.toStdString();
                if (!course.video.isEmpty())
                    res["video_url"] = course.video.toStdString();
                res["departments"] = course.departments.join(';').toStdString();

                // Add instructors to map.
                if (course.instructors.count() > 0)
                {
                    std::vector<sc::Variant> images, names, bios;
                    for (auto j = course.instructors.begin(); j != course.instructors.end(); ++j)
                    {
                        images.push_back(sc::Variant(j->image.toStdString()));
                        names.push_back(sc::Variant(j->name.toStdString()));
                        bios.push_back(sc::Variant((j->bio.length() < 150 ? j->bio : j->bio.left(150) + "...").toStdString()));
                    }
                    res["instr_images"] = images;
                    res["instr_names"] = names;
                    res["instr_bios"] = bios;
                }

                // Push the result
                if (!reply->push(res))
                    return;
            }

            qCDebug(Qry) << "Finished with source:" << sourceCatName;
        }

    } catch (domain_error &e) {
        // Handle exceptions being thrown by the client API
        cerr << e.what() << endl;
        reply->error(current_exception());
    }
}