コード例 #1
0
ファイル: query.cpp プロジェクト: QtRoS/academy
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());
    }
}
コード例 #2
0
ファイル: query.cpp プロジェクト: Anne017/academy
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());
    }
}