URL Shortener: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
m (→‎Source Code: Change c++ to cpp-qt to match edits mentioned here: https://forum.qt.io/topic/124460/qt-wiki-code-formatting)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:snippets]]<br />[[Category:HowTo]]
[[Category:snippets]]
 
[[Category:HowTo]]
[toc align_right=&quot;yes&amp;quot; depth=&quot;2&amp;quot;]
 
'''English''' [[URL_Shortener_Bulgarian|Български]]
 
= URL Shortener =
 
== Overview ==
== Overview ==


Line 13: Line 7:
== URL Shortener API ==
== URL Shortener API ==


There are plenty of URL Shortner API. The most popular are:<br />* &quot;Google Url Shortener&amp;quot;:http://goo.gl/<br />* &quot;Bit.ly&amp;quot;:http://dev.bitly.com/<br />* &quot;is.gd and v.gd&amp;quot;:http://is.gd/apishorteningreference.php
There are plenty of URL Shortner API. The most popular are:
* [http://goo.gl/ Google Url Shortener]
* [http://dev.bitly.com/ Bit.ly]
* [http://is.gd/apishorteningreference.php is.gd and v.gd]


== URL Shortener for Qt ==
== URL Shortener for Qt ==


Qt implementation of URL Shortener based on is.gd service and the &quot;file download class&amp;quot;:http://wiki.qt.io/Download_Data_from_URL.
Qt implementation of URL Shortener based on is.gd service and the [http://wiki.qt.io/Download_Data_from_URL file download class].


=== Source Code ===
=== Source Code ===


URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required. The provided implementation is from &quot;location2sms&amp;quot;:https://gitorious.org/location2sms (an open source mobile application).
URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required.  


<code><br />#ifndef URLSHORTENER_H<br />#define URLSHORTENER_H
<syntaxhighlight lang="cpp-qt">
#ifndef URLSHORTENER_H
#define URLSHORTENER_H


//Standard includes<br />#include &lt;QObject&amp;gt;<br />#include &lt;QString&amp;gt;
//Standard includes
#include <QObject>
#include <QString>


//Project specific includes<br />#include &quot;filedownloader.h&amp;quot;
//Project specific includes
#include "filedownloader.h"


class UrlShortener : public QObject<br />{<br /> Q_OBJECT
class UrlShortener : public QObject
{
Q_OBJECT


private:
private:
FileDownloader* m_pDownloader;
FileDownloader* m_pDownloader;
QString m_sShortUrl;
QString m_sShortUrl;


public:
public:
explicit UrlShortener(QObject *parent = 0);
virtual ~UrlShortener();
/*
* Request short URL from Google API
*
* param sURL
*
* return nothing
*/
void requestShortUrl(QString sURL);


explicit UrlShortener(QObject '''parent = 0);
/*
<br /> virtual ~UrlShortener();
* Get address
<br /> /'''*<br /> * Request short URL from Google API<br /> *<br /> * </code>param sURL<br /> *<br /> * <code>return nothing<br /> '''/<br /> void requestShortUrl(QString sURL);
*
<br /> /'''*<br /> * Get address<br /> *<br /> * </code>return QString<br /> */<br /> QString getShortUrl() const;
* return QString
*/
QString getShortUrl() const;


signals:
signals:
Line 49: Line 63:


private slots:
private slots:
void parseResponse();
};
#endif // URLSHORTENER_H
</syntaxhighlight>
<syntaxhighlight lang="cpp-qt">
//Project specific includes
#include "urlshortener.h"
//Standard includes
#include <QTextStream>


void parseResponse();
UrlShortener::UrlShortener(QObject *parent) :
QObject(parent),
m_pDownloader(NULL)
{
m_pDownloader = new FileDownloader(this);
connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
}
// ------------------


};
UrlShortener::~UrlShortener()
{
//Nothing to do
}
// ------------------


#endif // URLSHORTENER_H<br /><code>
void UrlShortener::requestShortUrl(QString sURL)
{
//Obtain short URL
QUrl req = QUrl( "http://is.gd/create.php");
req.addQueryItem("format", "simple");
req.addQueryItem("longurl", sURL);
m_pDownloader->downloadUrl(req);
}
// ------------------


</code>
void UrlShortener::parseResponse()
{
QTextStream downloadedStream(m_pDownloader->downloadedData());


//Project specific includes<br />#include &quot;urlshortener.h&amp;quot;
m_sShortUrl = downloadedStream.readAll();


//Standard includes<br />#include &lt;QTextStream&amp;gt;
//emit a signal
emit shortUrlRetrieved();
}
// ------------------


UrlShortener::UrlShortener(QObject '''parent) :<br /> QObject(parent),<br /> m_pDownloader(NULL)<br />{<br /> m_pDownloader = new FileDownloader(this);
QString UrlShortener::getShortUrl() const
<br /> connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));<br />}<br />//——————————————————————————
{
<br />UrlShortener::~UrlShortener()<br />{<br /> //Nothing to do<br />}<br />//——————————————————————————
return m_sShortUrl;
<br />void UrlShortener::requestShortUrl(QString sURL)<br />{<br /> //Obtain short URL<br /> QUrl req = QUrl( &quot;http://is.gd/create.php&amp;quot;);<br /> req.addQueryItem(&quot;format&amp;quot;, &quot;simple&amp;quot;);<br /> req.addQueryItem(&quot;longurl&amp;quot;, sURL);<br /> m_pDownloader-&gt;downloadUrl(req);<br />}<br />//——————————————————————————
}
<br />void UrlShortener::parseResponse()<br />{<br /> QTextStream downloadedStream(m_pDownloader-&gt;downloadedData());
// ------------------
<br /> m_sShortUrl = downloadedStream.readAll();
</syntaxhighlight>
<br /> //emit a signal<br /> emit shortUrlRetrieved();<br />}<br />//——————————————————————————
<br />QString UrlShortener::getShortUrl() const<br />{<br /> return m_sShortUrl;<br />}<br />//——————————————————————————
<br /><code>
<br />h3. Usage
<br /></code><br />m_pUrlShortener = new UrlShortener(this);<br />connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));<br />//'''<br />m_pUrlShortener-&gt;requestShortUrl(QString(&quot;http://example.com&amp;quot;));<br /><code>


== Related Articles ==
=== Usage ===
<code>
m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));
</code>

Latest revision as of 04:15, 7 June 2023

Overview

URL shortening is a technique in which a Uniform Resource Locator (URL) may be made substantially shorter in length and still direct to the required page. It is often used for simpler presentation of URLs at social networks and mobile applications.

URL Shortener API

There are plenty of URL Shortner API. The most popular are:

URL Shortener for Qt

Qt implementation of URL Shortener based on is.gd service and the file download class.

Source Code

URL Shortener API provided by is.gd supports json, xml and plain text as a simple response. In certain cases such as mobile applications the simple response is better solution because parsing is not required.

#ifndef URLSHORTENER_H
#define URLSHORTENER_H

//Standard includes
#include <QObject>
#include <QString>

//Project specific includes
#include "filedownloader.h"

class UrlShortener : public QObject
{
 Q_OBJECT

private:
FileDownloader* m_pDownloader;
QString m_sShortUrl;

public:
explicit UrlShortener(QObject *parent = 0);
virtual ~UrlShortener();
 /*
 * Request short URL from Google API
 *
 * param sURL
 *
 * return nothing
 */
 void requestShortUrl(QString sURL);

 /*
 * Get address
 *
 * return QString
 */
 QString getShortUrl() const;

signals:

void shortUrlRetrieved();

private slots:
void parseResponse();
};

#endif // URLSHORTENER_H
//Project specific includes
#include "urlshortener.h"

//Standard includes
#include <QTextStream>

UrlShortener::UrlShortener(QObject *parent) :
 QObject(parent),
 m_pDownloader(NULL)
{
 m_pDownloader = new FileDownloader(this);
 connect(m_pDownloader, SIGNAL (downloaded()), SLOT (parseResponse()));
}
// ------------------

UrlShortener::~UrlShortener()
{
 //Nothing to do
}
// ------------------

void UrlShortener::requestShortUrl(QString sURL)
{
 //Obtain short URL
 QUrl req = QUrl( "http://is.gd/create.php");
 req.addQueryItem("format", "simple");
 req.addQueryItem("longurl", sURL);
 m_pDownloader->downloadUrl(req);
}
// ------------------

void UrlShortener::parseResponse()
{
 QTextStream downloadedStream(m_pDownloader->downloadedData());

 m_sShortUrl = downloadedStream.readAll();

 //emit a signal
 emit shortUrlRetrieved();
}
// ------------------

QString UrlShortener::getShortUrl() const
{
 return m_sShortUrl;
}
// ------------------

Usage

m_pUrlShortener = new UrlShortener(this);
connect(m_pUrlShortener, SIGNAL (shortUrlRetrieved()), this, SLOT (loadMapShortUrl()));
//*
m_pUrlShortener->requestShortUrl(QString("http://example.com"));