Processing + XML (Web API)

  • ローカルのコンピュータやネットワックでの限られた情報を扱うだけでなく、Web に公開された情報との連携を可能にすることで、より汎用的で具体的な情報インタラクションを可能にすることが期待できる。
  • Web 上のコンテンツを個人的にアーカイブしていくウェブクリッピングサービス「 Tumblr 」が提供するAPI(Application Programming Interface)を利用することで、容易に Web 上のコンテンツを扱うことが可能になる。
  • Tumblr API は、HTTP リクエストをプログラムから送信することで XML ファイルをレスポンスします。
    レスポンスされた XML を Processing で解析しました。
  • Processing Library として公開されている proXML ライブラリを使うことで、HTTP リクエスト、XMLファイルの解析を行いました。
  • サンプルコード
    * 別ファイルとして TumblrToDB というクラスを用意しています。

[java]
//Tumblr member
import proxml.*;
XMLInOut xmlIO;
XMLElement tumblelog;
String id, type, url, dategmt;
String TumblrID, TumblrType, TumblrURL, TumblrDate, regularTitle, regularBody, quoteSource, quoteText, photoCaption, photoURL, linkText, linkURL;
int timekeeper;
int debounce = 59999; //reloades at 60,000millis.
//Tumblr member END

//TumblrToDB TTDB;

void setup(){
//Tumblr XML initialize
xmlIO = new XMLInOut(this);
xmlIO.loadElement(“http://inClip.tumblr.com/api/read”);

id = “id”;
type = “type”;
url = “url”;
dategmt = “date-gmt”;
//Tumblr XML initialize END

//TTDB = new TumblrToDB(this);

}

void draw(){
//XML reload
if(millis() – timekeeper > debounce){
xmlIO.loadElement(“http://inClip.tumblr.com/api/read”);
timekeeper = millis();
println(TTDB.gettimestamp());
}
//XML reload END//

}

///////////////////////////////////////////////
///////////////////////////////////////////////
void xmlEvent(XMLElement element){
//element.printElementTree();
tumblelog = element;
XMLElement tumblr;
XMLElement posts;

tumblr = tumblelog.getChild(0);

//TTDB = new TumblrToDB(this);

for(int i=1; i
posts = tumblelog.getChild(i);
/*String[] postsAttributes = posts.getAttributes();
for(int j=0; j

println(postsAttributes[j]);
}*/

XMLElement post;

for(int l=0; l
post = posts.getChild(l);
String[] postAttribute = post.getAttributes();
for(int m=0; m
if(id.equals(postAttribute[m]) == true) {
TumblrID = post.getAttribute(“id”);
println(“id : ” + post.getAttribute(“id”));
}
else if(type.equals(postAttribute[m]) == true) {
TumblrType = post.getAttribute(“type”);
println(“type : ” + post.getAttribute(“type”));
}
else if(url.equals(postAttribute[m]) == true) {
TumblrURL = post.getAttribute(“url”);
println(“url : ” + post.getAttribute(“url”));
}
else if(dategmt.equals(postAttribute[m]) == true) {
TumblrDate = post.getAttribute(“date-gmt”);
TumblrDate = reviseGMT(TumblrDate);
println(“date-gmt : ” + TumblrDate);
//println(“date-gmt : ” + post.getAttribute(“date-gmt”));
}
}
println();

String[] tx = new String[2];

XMLElement postElement;
for(int s=0; s
postElement = post.getChild(s);
if(postElement.firstChild().isTextElement() == true) {
if(postElement.toString().equals(“”)){
regularTitle = postElement.firstChild().getText();
//println(regularTitle);
}
if(postElement.toString().equals(“”)){
regularBody = postElement.firstChild().getText();
//println(regularBody);
}
if(postElement.toString().equals(“”)){
quoteSource = postElement.firstChild().getText();
//println(quoteSource);
}
if(postElement.toString().equals(“”)){
quoteText = postElement.firstChild().getText();
//println(quoteText);
}
if(postElement.toString().equals(”
“)){
photoCaption = postElement.firstChild().getText();
//println(photoCaption);
}
if(postElement.toString().equals(”
“)){
photoURL = postElement.firstChild().getText();
//println(photoURL);
}
if(postElement.toString().equals(”
“)){
linkText = postElement.firstChild().getText();
//println(linkText);
}
if(postElement.toString().equals(”
“)){
linkURL = postElement.firstChild().getText();
//println(linkURL);
}
}
}

//check TumblrDB exists
//println(TTDB.countRows(TumblrDate));
int cou = TTDB.countRows(TumblrDate);
if(TTDB.checkContents(TumblrDate) == false){
TTDB.getRGBtabel(cou ,TumblrDate);
for(int h=0; h
println(TTDB.getC(h));
/*if(TumblrType.equals(“regular”)){
mysql.setTumblrContents(TumblrID, TumblrType, TumblrURL, TumblrDate, regularTitle, regularBody);
//println(“this is \”regular\” content!”);
}
if(TumblrType.equals(“quote”)){
mysql.setTumblrContents(TumblrID, TumblrType, TumblrURL, TumblrDate, quoteSource, quoteText);
//println(“this is \”regular\” content!”);
}*/
if(TumblrType.equals(“photo”)){
TTDB.tableWritePhoto(int(TumblrID), TumblrType, TumblrURL, TumblrDate, photoCaption, photoURL, TTDB.getC(h), TTDB.getR(h), TTDB.getG(h), TTDB.getB(h));
//TTDB.setContents(TumblrID, TumblrType, TumblrURL, TumblrDate, photoCaption, photoURL);
//println(“this is \”photo\” content!”);
}
if(TumblrType.equals(“link”)){
//TTDB.setContents(TumblrID, TumblrType, TumblrURL, TumblrDate, linkText, linkURL);
println(“this is \”regular\” content!”);
}
}
}
}

}

/*
String root = element.toString();
println(root);
*/

//xmlIO.saveElement(element, “taggC0000.xml”);
}

///////////////////////////////////////////////
///////////////////////////////////////////////
String reviseGMT(String GMTDate){
String GMT = GMTDate;
String front = GMT.substring(0, 11);
String front2 = GMT.substring(0, 8);
String GMTDay = GMT.substring(8, 10);
String GMTHour = GMT.substring(11, 13);
String back = GMT.substring(13, 19);
int iGMT = int(GMTHour);
int iGMTDay = int(GMTDay);
iGMT += 9;
String reGMT;

if(iGMT > 24) {
iGMT -= 24;
iGMTDay += 1;

}

if(iGMT < 10 && iGMTDay <10) {
reGMT = front2 +”0″+ iGMTDay + ” 0″ +iGMT + back;
}
else{
reGMT = front + iGMT + back;
}
return reGMT;

}
[/java]

TumblrToDB クラスファイル

[java]
import de.bezier.mysql.*;
import java.sql.*;
import java.util.Date;

public class TumblrToDB {
//property
PApplet parent;
MySQL msql;

String table;
String RGBTable;

int ClipID;
int C, R, G, B;
int[] CC, RR, GG, BB;

//constructor
TumblrToDB(PApplet _p){
parent = _p;
String user     = “root”;
String pass     = “root”;
String database = “inClip”;
table    = “inClip_Tumblr_081106b”;
RGBTable = “inClip_Color_081106b”;

msql = new MySQL( “localhost:8889″, database, user, pass, _p );

CC = new int[3];
RR = new int[3];
GG = new int[3];
BB = new int[3];

if(msql.connect()){
msql.execute( “CREATE TABLE IF NOT EXISTS ” + table + ” (“+
“ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,”+
“TumblrID INT unsigned NOT NULL,”+
“Type TINYTEXT NOT NULL,”+
“URL varchar(200) NOT NULL,”+
“Date datetime default ’0000-00-00 00:00:00′ NOT NULL,”+
“regularTitle tinytext,”+
“regularBody longtext,”+
“quoteSource text,”+
“quoteText longtext,”+
“photoCaption text,”+
“photoURL varchar(200),”+
“linkText tinytext,”+
“linkURL varchar(200),”+
“C SMALLINT unsigned default 0,”+
“R SMALLINT unsigned default 0,”+
“G SMALLINT unsigned default 0,”+
“B SMALLINT unsigned default 0″+
“)”
);
}
}

//method
public void inputCRGB(int ClipID, int C, int R, int G, int B){
this.ClipID = ClipID;
this.C = C;
this.R = R;
this.G = G;
this.B = B;
}

public void tableWriteRegular(int TumblrID, String Type, String URL, String Date, String rT, String rB, int C, int R, int G, int B){
//if(myid.length() > 1) {
msql.execute( “INSERT INTO ” + table +
” (TumblrID, Type, URL, Date, regularTitle, regularBody, C, R, G, B) VALUES (” +
TumblrID +”, ” +
“\’”+ Type +”\’, ” +
“\’”+ URL +”\’, ” +
“\’”+ Date +”\’, ” +
“\’”+ rT +”\’, \’”+ rB +”\’, “+
C +”, ” + R +”, ” + G +”, ” + B +
“)” );
println(“sqlWrite—OK!”);
//tx.inputserial(myid, tarid, val1, val2, val3);
}

public void tableWriteQuote(int TumblrID, String Type, String URL, String Date, String qS, String qT, int C, int R, int G, int B){
msql.execute( “INSERT INTO ” + table +
” (TumblrID, Type, URL, Date, quoteSource, quoteText, C, R, G, B) VALUES (” +
TumblrID +”, ” +
“\’”+ Type +”\’, ” +
“\’”+ URL +”\’, ” +
“\’”+ Date +”\’, ” +
“\’”+qS +”\’, \’”+ qT +”\’, “+
C +”, ” + R +”, ” + G +”, ” + B +
“)” );
println(“sqlWrite—OK!”);
}

public void tableWritePhoto(int TumblrID, String Type, String URL, String Date, String pC, String pU, int C, int R, int G, int B){
msql.execute( “INSERT INTO ” + table +
” (TumblrID, Type, URL, Date, photoCaption, photoURL, C, R, G, B) VALUES (” +
TumblrID +”, ” +
“\’”+ Type +”\’, ” +
“\’”+ URL +”\’, ” +
“\’”+ Date +”\’, ” +
“\’”+pC +”\’, \’”+ pU +”\’, “+
C +”, ” + R +”, ” + G +”, ” + B +
“) ” );
println(“sqlWrite—OK!”);
}

public void tableWriteLink(int TumblrID, String Type, String URL, String Date, String lT, String lU, int C, int R, int G, int B){
msql.execute( “INSERT INTO ” + table +
” (TumblrID, Type, URL, Date, linkText, linkURL, C, R, G, B) VALUES (” +
TumblrID +”, ” +
“\’”+ Type +”\’, ” +
“\’”+ URL +”\’, ” +
“\’”+ Date +”\’, ” +
“\’”+lT +”\’, \’”+ lU +”\’, “+
C +”, ” + R +”, ” + G +”, ” + B +
“)” );
println(“sqlWrite—OK!”);
}

public String gettimestamp(){
msql.query(“select current_timestamp”);
msql.next();
Timestamp sqlts = msql.getTimestamp(“current_timestamp”);
String ts = sqlts + “”;
return ts;
}

////

public boolean checkContents(String TumblrDate){
boolean ex = false;
String t = TumblrDate.substring(0, 18);
msql.query(“select * from ” + table + ” where Date Like \’”+ t +”_\’”);
if(msql.next() == false) ex = false;  //println(“null”);
if(msql.next() == true) ex = true;  //println(“Exist!”);
/*while(msql.next()){
Timestamp ts = msql.getTimestamp(“Date”);
println(ts);
}*/
return ex;
}

int rowNumber;
public int countRows(String TumblrDate){
String t = TumblrDate;
msql.query( “select count(*) from ” + RGBTable + ” where Date = \’”+ t +”\’ “);
while(msql.next()){
rowNumber = msql.getInt( “count(*)” );

}
return rowNumber;
}

public void getRGBtabel(int n, String TumblrDate){
String t = TumblrDate;
msql.query( “select * from ” + RGBTable + ” where Date = \’”+ t +”\’ “);
int count = 0;
while(msql.next()){
ClipID = msql.getInt(“ClipID”);
CC[count] = msql.getInt(“C”);
RR[count] = msql.getInt(“R”);
GG[count] = msql.getInt(“G”);
BB[count] = msql.getInt(“B”);
count++;
}
}

public int getC(int h){
return CC[h];
}
public int getR(int h){
return RR[h];
}
public int getG(int h){
return GG[h];
}
public int getB(int h){
return BB[h];
}

public void setContents(String TID, String TType, String TURL, String TDate, String contentTitle, String contentBody){
String t = TumblrDate;
int iTID = int(TID);
msql.query(“select * from ” + RGBTable + ” where Date = \’”+ t +”\’”);
while(msql.next()){
int ClipID = msql.getInt(“ClipID”);
int C = msql.getInt(“C”);
int R = msql.getInt(“R”);
int G = msql.getInt(“G”);
int B = msql.getInt(“B”);
//Timestamp ts = msql.getTimestamp(“Date”);
//println(“ClipID:”+ClipID+”, R:”+r+”, G:”+g+”, B:”+b+”, time:”+ts);
/*if(TType.equals(“regular”)) tableWriteRegular(iTID, TType, TURL, TDate, contentTitle, contentBody, c, r, g, b);
if(TType.equals(“quote”)) tableWriteQuote(iTID, TType, TURL, TDate, contentTitle, contentBody, c, r, g, b);
if(TType.equals(“photo”)) tableWritePhoto(iTID, TType, TURL, TDate, contentTitle, contentBody, c, r, g, b);
if(TType.equals(“link”)) tableWriteLink(iTID, TType, TURL, TDate, contentTitle, contentBody, c, r, g, b);*/

}
}
}
[/java]

コメントをどうぞ