Skip to content

Instantly share code, notes, and snippets.

@mallain
Created September 12, 2011 21:26
Show Gist options
  • Save mallain/1212491 to your computer and use it in GitHub Desktop.
Save mallain/1212491 to your computer and use it in GitHub Desktop.
Extract from my QVSource Yammer Connector
/*******************************************************************************
* Write Table for Messages Table Yammer
* *****************************************************************************/
private DataTable writeTableMessages(IProgress prog, ITableWriter tblWriter)
{
if (_dtMessages == null)
{
// Requete sur API Yammer
string res = _oauth.Get("https://www.yammer.com/api/v1/messages.json");
// Convert
XmlDocument xmlD = this.DataHelper.ConvertJsonToXml(res);
DataTable dt = null;
// Creation d une DataTable
dt = new DataTable();
dt.Columns.Add("network_id");
dt.Columns.Add("thread_id");
dt.Columns.Add("sender_id");
dt.Columns.Add("client_type");
dt.Columns.Add("created_at");
dt.Columns.Add("privacy");
dt.Columns.Add("url");
// Recuperation des messages
XmlNodeList xnList = xmlD.SelectNodes("/DATA/messages");
// Total of rows
int NO_ROWS = xnList.Count;
// Compteur
int i = 1;
foreach (XmlNode xn in xnList)
{
string network_id = xn["network_id"].InnerText;
string thread_id = xn["thread_id"].InnerText;
string sender_id = xn["sender_id"].InnerText;
string client_type = xn["client_type"].InnerText;
string created_at = xn["created_at"].InnerText;
string privacy = xn["privacy"].InnerText;
string url = xn["url"].InnerText;
// Ajout de la ligne avec les attributs
dt.Rows.Add(network_id, thread_id, sender_id, client_type, created_at, privacy, url);
prog.Set(NO_ROWS, i, "Downloading data " + i + " of " + NO_ROWS);
// Check if the user has cancelled the operation and if
// so abort.
if (prog.Cancelled) break;
}
// Retourne le DataTable
_dtMessages = dt;
}
// Ecriture sur le Control QVSource
tblWriter.WriteTable(_dtMessages);
// Retourne le DataTable
return _dtMessages;
}
/*******************************************************************************
* Write Table for Networks Table Yammer
******************************************************************************/
private DataTable writeTableNetworks(IProgress prog, ITableWriter tblWriter)
{
if (_dtNetworks == null)
{
// Requete sur API Yammer
string res = _oauth.Get("https://www.yammer.com/api/v1/networks/current.json");
// Convert JSON en XML (Ajout d une balise ROOT pour pouvoir parser correctement le JSON)
XmlDocument xmlD = this.DataHelper.ConvertJsonToXml("{\"root\":" + res + "}");
DataTable dt = null;
// Creation d une DataTable
dt = new DataTable();
dt.Columns.Add("network_id");
dt.Columns.Add("network_name");
dt.Columns.Add("network_type");
dt.Columns.Add("unseen_message_count");
dt.Columns.Add("network_web_url");
dt.Columns.Add("network_permalink");
// Recuperation des messages
XmlNodeList xnList = xmlD.SelectNodes("/DATA/root");
// Total of rows
int NO_ROWS = xnList.Count;
// Compteur
int i = 1;
foreach (XmlNode xn in xnList)
{
// Element sous Root
string network_id = xn["id"].InnerText;
string network_name = xn["name"].InnerText;
string network_type = xn["type"].InnerText;
string unseen_message_count = xn["unseen_message_count"].InnerText;
string network_web_url = xn["web_url"].InnerText;
string network_permalink = xn["permalink"].InnerText;
// Ajout de la ligne avec les attributs
dt.Rows.Add(network_id, network_name, network_type, unseen_message_count, network_web_url, network_permalink);
prog.Set(NO_ROWS, i, "Downloading data " + i + " of " + NO_ROWS);
// Check if the user has cancelled the operation and if
// so abort.
if (prog.Cancelled) break;
}
// Retourne le DataTable
_dtNetworks = dt;
}
// Ecriture sur le Control QVSource
tblWriter.WriteTable(_dtNetworks);
// Retourne le DataTable
return _dtNetworks;
}
/*******************************************************************************
* Write Table for Users Table Yammer
******************************************************************************/
private DataTable writeTableUsers(IProgress prog, ITableWriter tblWriter)
{
int userPerPage = 50;
int pageNumber = 1;
bool loopFinish = false;
// Instanciation d une DataTable
DataTable dt = null;
// Creation d une DataTable
dt = new DataTable();
dt.Columns.Add("network_id");
dt.Columns.Add("user_id");
dt.Columns.Add("user_type");
dt.Columns.Add("user_admin");
dt.Columns.Add("user_job_title");
dt.Columns.Add("user_name");
dt.Columns.Add("user_fullname");
dt.Columns.Add("user_web_url");
dt.Columns.Add("user_mugshot_url");
dt.Columns.Add("user_state");
dt.Columns.Add("user_location");
dt.Columns.Add("user_birthdate");
dt.Columns.Add("user_summary");
dt.Columns.Add("user_expertise");
dt.Columns.Add("user_timezone");
if (_dtUsers == null)
{
// Tant qu il y a des utilisateurs a recuperer
while (!loopFinish)
{
// Requete sur API Yammer
string urlGet = "https://www.yammer.com/api/v1/users.json?page=" + pageNumber;
string res = _oauth.Get(urlGet);
// Convert JSON en XML (Ajout d une balise ROOT pour pouvoir parser correctement le JSON)
XmlDocument xmlD = this.DataHelper.ConvertJsonToXml("{\"root\":" + res + "}");
// Recuperation des utilisateurs
XmlNodeList xnList = xmlD.SelectNodes("/DATA/root");
// Total of rows
int NO_ROWS = xnList.Count;
// Test si derniere page
if (NO_ROWS < userPerPage)
loopFinish = true;
// Compteur
int i = 1;
foreach (XmlNode xn in xnList)
{
// Element sous Root
string network_id = xn["network_id"].InnerText;
string user_id = xn["id"].InnerText;
string user_type = xn["type"].InnerText;
string user_admin = xn["admin"].InnerText;
string user_job_title = xn["job_title"].InnerText;
string user_name = xn["name"].InnerText;
string user_fullname = xn["full_name"].InnerText;
string user_web_url = xn["web_url"].InnerText;
string user_mugshot_url = xn["mugshot_url"].InnerText;
string user_state = xn["state"].InnerText;
string user_location = xn["location"].InnerText;
string user_birthdate = xn["birth_date"].InnerText;
string user_summary = xn["summary"].InnerText;
string user_expertise = xn["expertise"].InnerText;
string user_timezone = xn["timezone"].InnerText;
// Recuperation des elements sous stats
XmlNodeList xnStats = xn.SelectNodes("/stats");
string user_followers = xn["followers"].InnerText;
string user_following = xn["following"].InnerText;
string user_updates = xn["updates"].InnerText;
// Ajout de la ligne avec les attributs
dt.Rows.Add(network_id, user_id, user_type, user_admin, user_job_title, user_name, user_fullname, user_web_url,
user_mugshot_url,
user_state,
user_location,
user_birthdate,
user_summary,
user_expertise,
user_timezone,
user_followers,
user_following,
user_updates
);
prog.Set(NO_ROWS, i, "Downloading data " + i + " of " + NO_ROWS);
// Check if the user has cancelled the operation and if
// so abort.
if (prog.Cancelled) break;
}
// Increment pageNumber
pageNumber += 1;
}
// Retourne le DataTable
_dtUsers = dt;
}
// Ecriture sur le Control QVSource
tblWriter.WriteTable(_dtUsers);
// Retourne le DataTable
return _dtUsers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment