Changeset 461:6282b93fcfca in SMSSender for gateways/CoopMobile/business/contactimporter/contactimporter.cpp


Ignore:
Timestamp:
Jan 5, 2015 2:55:55 PM (4 years ago)
Author:
Sämy Zehnder <saemy.zehnder@…>
Branch:
new-gtwy-coopmobile
Message:

Implements the contact importer for the CoopMobile? gateway.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gateways/CoopMobile/business/contactimporter/contactimporter.cpp

    r454 r461  
    11/*
    22  Coop Mobile gateway plugin - The smssender plugin for the Coop Mobile platform.
    3   Copyright (C) 2011-2014, gorrión. See http://smssender.gorrion.ch
     3  Copyright (C) 2014-2015, gorrión. See http://smssender.gorrion.ch
    44
    55  This program is free software: you can redistribute it and/or modify
     
    1818#include "contactimporter.h"
    1919
     20#include <QRegExp>
     21
    2022#include "common/domain/scontact.h"
    2123#include "common/domain/snumber.h"
     
    2628namespace CoopMobile {
    2729
    28 const QString ContactImporter::URL_EXPORT_CONTACTS =
    29         "https://organizer.sso.post.ch/addresses/adr_csv_export_action.html";
    30 
    3130ContactImporter::ContactImporter(Account *account)
    3231    : m_account(account)
     
    3534
    3635util::Status ContactImporter::importContacts(QSet<SContact *> *contacts) {
    37     // Fetch the contacts
    38     SNetworkReply reply;
    39     STATUS_RETURN_IF_ERROR(
    40                 m_account->httpHelper()->syncGet(URL_EXPORT_CONTACTS, &reply));
    41     QString csv = reply->readAll();
    42     csv.replace("\r", "");
     36    // Fetches the contacts.
     37    QString nextUrl = Gateway::URL_ADDRESS_BOOK;
     38    int pageId = 1;
     39    do {
     40        SNetworkReply reply;
     41        STATUS_RETURN_IF_ERROR(m_account->httpHelper()->syncGet(nextUrl, &reply));
     42        QString html = QString::fromUtf8(reply->readAll());
    4343
    44     // Split csv into rows
    45     QStringList rows = csv.split('\n', QString::SkipEmptyParts);
     44        STATUS_RETURN_IF_ERROR(parseContacts(html, contacts));
     45        // TODO: Parse the groups.
    4646
    47     // Get the interresting columns
    48     QString columnDescriptorRow = rows.takeFirst();
    49     QStringList columnDescriptor = splitIntoCells(columnDescriptorRow);
    50     int prenameCol = columnDescriptor.indexOf(QRegExp("^Vorname$"));
    51     int lastnameCol = columnDescriptor.indexOf(QRegExp("^Nachname$"));
    52     int mobileCol = columnDescriptor.indexOf(QRegExp("^Mobil-Nummer$"));
    53     int mobileCol2 = columnDescriptor.indexOf(QRegExp("^Mobil-Nummer at$"));
    54 
    55 
    56     // Parse the contacts
    57     foreach (QString row, rows) {
    58         QStringList contactData = splitIntoCells(row);
    59 
    60         QString name = QString("%1 %2")
    61                 .arg(contactData.value(prenameCol))
    62                 .arg(contactData.value(lastnameCol))
    63                 .trimmed();
    64         SNumber number(contactData.value(mobileCol));
    65         if (!number.isValid()) {
    66             number = SNumber(contactData.value(mobileCol2));
    67         }
    68 
    69         if (!name.isEmpty() && number.isValid()) {
    70             QScopedPointer<SContact> contact(new SContact);
    71             contact->setName(name);
    72             contact->setNumber(number);
    73 
    74             contacts->insert(contact.take());
    75         }
    76     }
     47        // Fetches the url of the next page.
     48        QString nextUrlTmp =
     49                QString("/contacts/index/lang/de/page/%1").arg(++pageId);
     50        nextUrl = html.contains(QString("<a id=\"nextpage\" href=\"%1\">").arg(nextUrlTmp))
     51            ? Gateway::URL_BASE + nextUrlTmp
     52            : "";
     53    } while (!nextUrl.isEmpty());
    7754
    7855    return util::StatusOk();
    7956}
    8057
    81 QStringList ContactImporter::splitIntoCells(QString csvRow) {
    82     csvRow = csvRow.mid(1, csvRow.length()-2); // Remove first and last "
    83     return csvRow.split(QRegExp("\";\""), QString::KeepEmptyParts);
     58
     59static util::Status readField(const QString &html, const QString &name,
     60                              QString *value) {
     61    QRegExp rx(QString("<input type=\"text\" name=\"%1\" value=\"([^\"]+)\" />")
     62               .arg(name));
     63    if (rx.indexIn(html) == -1) {
     64        return util::StatusParseError(
     65                    QString("Couldn't find the '%1' field.").arg(name), html);
     66    }
     67
     68    *value = rx.cap(1);
     69    return util::StatusOk();
     70}
     71
     72util::Status ContactImporter::parseContacts(const QString &html,
     73                                            QSet<SContact *> *contacts) {
     74    // Parses the contacts.
     75    QRegExp rx("contactCheck_(\\d+)");
     76    int pos = 0;
     77    while ((pos = rx.indexIn(html, pos)) != -1) {
     78        pos += rx.matchedLength();
     79
     80        QString contactId = rx.cap(1);
     81        QString contactUrl =
     82                QString("https://coop.nth.ch/contacts/edit/contact_id/%1/lang/de")
     83                    .arg(contactId);
     84
     85        SNetworkReply reply;
     86        STATUS_RETURN_IF_ERROR(
     87                    m_account->httpHelper()->syncGet(contactUrl, &reply));
     88        QString contactHtml = QString::fromUtf8(reply->readAll());
     89
     90        // Parses the current contact.
     91        QString prename;
     92        QString lastname;
     93        QString numberStr;
     94        STATUS_RETURN_IF_ERROR(readField(contactHtml, "first_name", &prename));
     95        STATUS_RETURN_IF_ERROR(readField(contactHtml, "last_name", &lastname));
     96        STATUS_RETURN_IF_ERROR(readField(contactHtml, "mobile", &numberStr));
     97
     98        // Creates and saves the contact.
     99        QScopedPointer<SContact> contact(new SContact);
     100        contact->setName(QString("%1 %2").arg(prename).arg(lastname));
     101        contact->setNumber(SNumber(numberStr, "'00'ccaau"));
     102
     103        contacts->insert(contact.take());
     104    }
     105
     106    return util::StatusOk();
    84107}
    85108
     
    93116QString ContactImporter::Builder::description() const {
    94117    if (isApplicable()) {
    95         return QObject::tr("Import contacts from the Post website.");
     118        return QObject::tr("Import contacts from the CoopMobile website.");
    96119    } else {
    97         return QObject::tr("Import contacts from the Post website. (No logged in accounts)");
     120        return QObject::tr("Import contacts from the CoopMobile website. (No logged in accounts)");
    98121    }
    99122}
Note: See TracChangeset for help on using the changeset viewer.