Add a convenience function to do the parsing, handling XML and JSON
cases
This commit is contained in:
		
					parent
					
						
							
								ed7b5b06d3
							
						
					
				
			
			
				commit
				
					
						f442c2f605
					
				
			
		
					 2 changed files with 75 additions and 0 deletions
				
			
		| 
						 | 
					@ -21,6 +21,9 @@
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "utils.h"
 | 
					#include "utils.h"
 | 
				
			||||||
 | 
					#include <qjson/parser.h>
 | 
				
			||||||
 | 
					#include <QtXml/QXmlStreamReader>
 | 
				
			||||||
 | 
					#include <QDebug>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace Danbooru {
 | 
					namespace Danbooru {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -101,5 +104,68 @@ namespace Danbooru {
 | 
				
			||||||
        return danbooruUrl;
 | 
					        return danbooruUrl;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QList< QVariant > parseDanbooruResult(QByteArray data, QString elementName,
 | 
				
			||||||
 | 
					                                          bool* result)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QXmlStreamReader reader;
 | 
				
			||||||
 | 
					        reader.addData(data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QList<QVariant> postData;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        while(!reader.atEnd() && !reader.hasError()) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            QXmlStreamReader::TokenType token = reader.readNext();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (token == QXmlStreamReader::StartDocument) {
 | 
				
			||||||
 | 
					                continue;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if(token == QXmlStreamReader::StartElement &&
 | 
				
			||||||
 | 
					               reader.name() == elementName)  {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                QVariantMap values;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                QXmlStreamAttributes attributes = reader.attributes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//                 qDebug() << attributes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                Q_FOREACH(const QXmlStreamAttribute& attribute, attributes) {
 | 
				
			||||||
 | 
					                    values.insert(attribute.name().toString(),
 | 
				
			||||||
 | 
					                                  attribute.value().toString());
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (values.isEmpty()) {
 | 
				
			||||||
 | 
					                    *result = false;
 | 
				
			||||||
 | 
					                    return QList<QVariant>();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                QVariant converted = QVariant(values);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                postData.append(converted);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        *result = true;
 | 
				
			||||||
 | 
					        return postData;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QList< QVariant > parseDanbooruResult(QByteArray data, bool* result)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        QJson::Parser parser;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bool ok;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        QVariant postData = parser.parse(data, &ok);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!ok) {
 | 
				
			||||||
 | 
					            *result = ok;
 | 
				
			||||||
 | 
					            return QList<QVariant>();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        *result = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return postData.toList();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
} // namespace Danbooru
 | 
					} // namespace Danbooru
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,8 +23,12 @@
 | 
				
			||||||
#ifndef UTILS_H
 | 
					#ifndef UTILS_H
 | 
				
			||||||
#define UTILS_H
 | 
					#define UTILS_H
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Qt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <QtCore/QStringList>
 | 
					#include <QtCore/QStringList>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// KDE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <kurl.h>
 | 
					#include <kurl.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
| 
						 | 
					@ -100,5 +104,10 @@ namespace Danbooru {
 | 
				
			||||||
                    const QString& password);
 | 
					                    const QString& password);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    QList<QVariant> parseDanbooruResult(QByteArray data, QString xlmElement,
 | 
				
			||||||
 | 
					                                        bool* result);
 | 
				
			||||||
 | 
					    QList<QVariant> parseDanbooruResult(QByteArray data, bool* result);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#endif // UTILS_H
 | 
					#endif // UTILS_H
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue