Add parsing functions
This commit is contained in:
		
					parent
					
						
							
								9b37769c62
							
						
					
				
			
			
				commit
				
					
						65a35113f7
					
				
			
		
					 2 changed files with 132 additions and 49 deletions
				
			
		| 
						 | 
					@ -31,6 +31,22 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "libdanbooru_debug.h"
 | 
					#include "libdanbooru_debug.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					typedef QLatin1String QL1S;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const QMap<QString, Danbooru::Rating> RATING_MAP = Danbooru::initRatingMap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const QMap< QString, Danbooru::Rating > Danbooru::initRatingMap()
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QMap<QString, Danbooru::Rating> map;
 | 
				
			||||||
 | 
					    map.insert("s", Safe);
 | 
				
			||||||
 | 
					    map.insert("q", Questionable);
 | 
				
			||||||
 | 
					    map.insert("e", Explicit);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace {
 | 
					namespace {
 | 
				
			||||||
    QUrl adjustUrl(QUrl baseUrl, QUrl boardUrl) {
 | 
					    QUrl adjustUrl(QUrl baseUrl, QUrl boardUrl) {
 | 
				
			||||||
        if(baseUrl.host().isEmpty()) {
 | 
					        if(baseUrl.host().isEmpty()) {
 | 
				
			||||||
| 
						 | 
					@ -44,19 +60,7 @@ namespace {
 | 
				
			||||||
namespace Danbooru
 | 
					namespace Danbooru
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
static const QMap<QString, Rating> RATING_MAP = initRatingMap();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const QMap< QString, Danbooru::Rating > initRatingMap()
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    QMap<QString, Danbooru::Rating> map;
 | 
					 | 
				
			||||||
    map.insert("s", Safe);
 | 
					 | 
				
			||||||
    map.insert("q", Questionable);
 | 
					 | 
				
			||||||
    map.insert("e", Explicit);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return map;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
QUrl requestUrl(QUrl &url, const QString &path,
 | 
					QUrl requestUrl(QUrl &url, const QString &path,
 | 
				
			||||||
                const QString &username, const QString &password,
 | 
					                const QString &username, const QString &password,
 | 
				
			||||||
| 
						 | 
					@ -221,6 +225,8 @@ QVariant parseDanbooruResult(QByteArray data, bool *result)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    QVariant postData = parsed.toVariant();
 | 
					    QVariant postData = parsed.toVariant();
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					//     qCDebug(LIBDANBOORU) << "Raw JSON response" << parsed.toJson(QJsonDocument::Indented).constData();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    *result = true;
 | 
					    *result = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return postData;
 | 
					    return postData;
 | 
				
			||||||
| 
						 | 
					@ -257,16 +263,43 @@ void fixPostUrl(DanbooruPost *post, QUrl boardUrl) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QVariantMap parseResult(const QByteArray &data, ApiType type) {
 | 
					QVariantMap extractPoolData(const QVariant &data, ApiType type) {
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    QVariantMap map;
 | 
					    auto mapped = data.toMap();
 | 
				
			||||||
    bool result;
 | 
					    int id;
 | 
				
			||||||
    QVariantMap mapped = parseDanbooruResult(data, &result).toMap();
 | 
					    QString name;
 | 
				
			||||||
 | 
					    int postCount;
 | 
				
			||||||
 | 
					    QString description;
 | 
				
			||||||
 | 
					    QVariantList rawData;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    id = mapped.value("id").toInt();
 | 
				
			||||||
 | 
					    name = mapped.value("name").toString();
 | 
				
			||||||
 | 
					    postCount = mapped.value("post_count").toInt();
 | 
				
			||||||
 | 
					    description = mapped.value("description").toString();
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    switch(type) {
 | 
				
			||||||
 | 
					        case MoeBooru:
 | 
				
			||||||
 | 
					            rawData = mapped.value("posts").toList();
 | 
				
			||||||
 | 
					            break;
 | 
				
			||||||
 | 
					        default:
 | 
				
			||||||
 | 
					            rawData = mapped.value("post_ids").toList();
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    QVariantMap map = {
 | 
				
			||||||
 | 
					        {"id", id},
 | 
				
			||||||
 | 
					        {"post_count", postCount},
 | 
				
			||||||
 | 
					        {"name", name},
 | 
				
			||||||
 | 
					        {"description", description},
 | 
				
			||||||
 | 
					        {"raw_post_data", rawData}
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return map;
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    if (!result) {
 | 
					 | 
				
			||||||
        return QVariantMap();
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QVariantMap extractPostData(const QVariant &data, ApiType type) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    auto mapped = data.toMap();
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    int id;
 | 
					    int id;
 | 
				
			||||||
    QSet<QString> tags;
 | 
					    QSet<QString> tags;
 | 
				
			||||||
    int width;
 | 
					    int width;
 | 
				
			||||||
| 
						 | 
					@ -301,13 +334,13 @@ QVariantMap parseResult(const QByteArray &data, ApiType type) {
 | 
				
			||||||
            rating = Danbooru::Safe;
 | 
					            rating = Danbooru::Safe;
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
        default:
 | 
					        default:
 | 
				
			||||||
            auto tagKey = mapped.contains("tag_string") ? QLatin1String("tag_string"): QLatin1String("tag-string");
 | 
					            auto tagKey = mapped.contains("tag_string") ? QL1S("tag_string"): QL1S("tag-string");
 | 
				
			||||||
            auto widthKey = mapped.contains("image_width") ? QLatin1String("image_width"): QLatin1String("image-width");
 | 
					            auto widthKey = mapped.contains("image_width") ? QL1S("image_width"): QL1S("image-width");
 | 
				
			||||||
            auto heightKey = mapped.contains("image_height") ? QLatin1String("image_height"): QLatin1String("image-height");
 | 
					            auto heightKey = mapped.contains("image_height") ? QL1S("image_height"): QL1S("image-height");
 | 
				
			||||||
            auto fileSizeKey = mapped.contains("file_size") ? QLatin1String("file_size"): QLatin1String("file-size");
 | 
					            auto fileSizeKey = mapped.contains("file_size") ? QL1S("file_size"): QL1S("file-size");
 | 
				
			||||||
            auto fileUrlKey = mapped.contains("file_url") ? QLatin1String("file_url"): QLatin1String("file-url");
 | 
					            auto fileUrlKey = mapped.contains("file_url") ? QL1S("file_url"): QL1S("file-url");
 | 
				
			||||||
            auto thumbnailUrlKey = mapped.contains("preview_file_url") ? QLatin1String("preview_file_url"): QLatin1String("preview-file-url");
 | 
					            auto thumbnailUrlKey = mapped.contains("preview_file_url") ? QL1S("preview_file_url"): QL1S("preview-file-url");
 | 
				
			||||||
            auto sampleUrlKey = mapped.contains("large_file_url") ? QLatin1String("large_file_url"): QLatin1String("large-file-url");
 | 
					            auto sampleUrlKey = mapped.contains("large_file_url") ? QL1S("large_file_url"): QL1S("large-file-url");
 | 
				
			||||||
            
 | 
					            
 | 
				
			||||||
            id = mapped.value("id").toString().toInt();
 | 
					            id = mapped.value("id").toString().toInt();
 | 
				
			||||||
            tags = QSet<QString>::fromList(mapped.value(tagKey).toString().split(' '));
 | 
					            tags = QSet<QString>::fromList(mapped.value(tagKey).toString().split(' '));
 | 
				
			||||||
| 
						 | 
					@ -321,18 +354,65 @@ QVariantMap parseResult(const QByteArray &data, ApiType type) {
 | 
				
			||||||
            break;
 | 
					            break;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    map.insert(QLatin1String("width"), width);
 | 
					        
 | 
				
			||||||
    map.insert(QLatin1String("height"), height);
 | 
					    QVariantMap map = {
 | 
				
			||||||
    map.insert(QLatin1String("id"), id);
 | 
					        {QL1S("width"), width}, {QL1S("height"), height}, {QL1S("id"), id}, 
 | 
				
			||||||
    map.insert(QLatin1String("url"), url);
 | 
					        {QL1S("url"), url}, {QL1S("thumbnailUrl"), thumbnailUrl},
 | 
				
			||||||
    map.insert(QLatin1String("thumbnailUrl"), url);
 | 
					        {QL1S("sampleUrl"), sampleUrl}, {QL1S("rating"), rating}, 
 | 
				
			||||||
    map.insert(QLatin1String("sampleUrl"), sampleUrl);
 | 
					        {QL1S("size"), fileSize}, {QL1S("tags"), QVariant::fromValue(tags)}
 | 
				
			||||||
    map.insert(QLatin1String("rating"), rating);
 | 
					    };
 | 
				
			||||||
    map.insert(QLatin1String("size"), fileSize);
 | 
					 | 
				
			||||||
    map.insert(QLatin1String("tags"), QVariant::fromValue(tags));
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    return(map);
 | 
					    return(map);
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QList<QVariantMap> parseResult(const QByteArray &data, ApiType type, Danbooru::Request request, bool *result) {
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    QVariantMap map;
 | 
				
			||||||
 | 
					    bool ok;
 | 
				
			||||||
 | 
					    QVariant rawMapped = parseDanbooruResult(data, &ok);
 | 
				
			||||||
 | 
					    QList<QVariantMap> postList;
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if (!ok) {
 | 
				
			||||||
 | 
					        *result = false;
 | 
				
			||||||
 | 
					        return QList<QVariantMap>();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    QVariantList mapped = rawMapped.toList();
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    // For single queries like pools
 | 
				
			||||||
 | 
					    if (mapped.isEmpty()) {
 | 
				
			||||||
 | 
					        mapped.append(rawMapped.toMap());
 | 
				
			||||||
 | 
					    } 
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    for (const auto element : mapped) {
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        QVariantMap mapped;
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        switch(request) {
 | 
				
			||||||
 | 
					            case Pool:
 | 
				
			||||||
 | 
					                mapped = extractPoolData(element, type);
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            case Artist:
 | 
				
			||||||
 | 
					                qDebug() << "Reimplement";
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            case Tag:
 | 
				
			||||||
 | 
					                qDebug() << "Reimplement";
 | 
				
			||||||
 | 
					                break;
 | 
				
			||||||
 | 
					            default:
 | 
				
			||||||
 | 
					                mapped = extractPostData(element, type);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        postList.append(mapped);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    *result = true; 
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    return postList;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					QList<QVariantMap> parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result) {
 | 
				
			||||||
 | 
					    return QList<QVariantMap>();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace Danbooru
 | 
					} // namespace Danbooru
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -120,10 +120,13 @@ bool isPostBlacklisted(DanbooruPost *post, QSet<QString> blacklist, Danbooru::Ra
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void fixPostUrl(DanbooruPost *post, QUrl boardUrl);
 | 
					void fixPostUrl(DanbooruPost *post, QUrl boardUrl);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
QVariantMap parseResult(const QByteArray &data, Danbooru::ApiType type);
 | 
					QList<QVariantMap> parseResult(const QByteArray &data, Danbooru::ApiType type, Danbooru::Request request, bool *result);
 | 
				
			||||||
QVariantMap parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type);
 | 
					QList<QVariantMap> parseResult(const QXmlStreamAttributes &data, Danbooru::ApiType type, bool *result);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const QMap< QString, Danbooru::Rating > initRatingMap();
 | 
					QVariantMap extractPostData(const QVariant &data, ApiType type);
 | 
				
			||||||
 | 
					QVariantMap extractPoolData(const QVariant &data, ApiType type);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const QMap< QString, Danbooru::Rating > initRatingMap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue