This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hughrawlinsonfacebook.pde
154 lines (120 loc) · 3.69 KB
/
hughrawlinsonfacebook.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import java.io.File;
// application api key and secret
String fbApiKey = "341862115846576";
String fbApiSecret = "d69f58ca2ba4cfc40ac340f61eac1475";
// a comma separated (no spaces!) list of user ids
String fbUserIDs = "1123363239";
/* other settings */
//
// Facebook RESTful API
String fbRestServer = "https://api.facebook.com/";
String fbRestNode = "restserver.php";
XML[] usersXml;
int currentUser = 0;
void setup ()
{
size( 300, 200 );
// the details / params in here define what will be read, see:
// http://wiki.developers.facebook.com/index.php/Users.getInfo
String xmlResponse = fbCallMethod( new String[] {
"method=facebook.friends.get",
"uid=" + fbUserIDs,
"fields=uid,first_name,last_name", // see link above for more options
"format=XML"
}
);
PrintWriter writer = createWriter("xml.txt");
writer.print(xmlResponse);
writer.flush();
writer.close();
if ( xmlResponse == null ) // an error occured
{
exit();
return;
}
XML xml;
xml = loadXML("xml.txt");
File f = new File(dataPath("xml.txt"));
f.delete();
usersXml = xml.getChildren( "uid" );
for (int i = 0; i < usersXml.length; i++) {
String friendData = fbCallMethod( new String[] {
"method=facebook.users.getInfo",
"uid=" + usersXml[i].getContent(),
"fields=uid,first_name,last_name,birthday_date", // see link above for more options
"format=XML"
});
PrintWriter writer2 = createWriter(usersXml[i].getContent()+".txt");
writer2.print(friendData);
writer2.flush();
writer2.close();
XML xml2 = loadXML(usersXml[i].getContent()+".txt");
println(xml2.getContent());
}
fill( 0 );
textFont( createFont( "sans-serif", 24 ) );
textAlign( CENTER );
frameRate( 1 );
}
void draw ()
{
background( 255 );
String full_name = usersXml[currentUser].getContent();
text( full_name, width/2, height/2 );
currentUser++;
currentUser %= usersXml.length; // modulo, wrap around
}
/**
* Place a Facebook call (GET request) using Processing API ( loadStrings(), join() )
*/
String fbCallMethod ( String[] args )
{
String[] params = new String[args.length + 3];
System.arraycopy( args, 0, params, 0, args.length );
params[params.length-3] = "api_key=" + fbApiKey;
params[params.length-2] = "call_id=" + System.currentTimeMillis();
params[params.length-1] = "v=1.0";
String sig = fbGenerateSIG ( params );
String paramString = join( params, "&" ) + "&sig=" + sig;
String[] lines = loadStrings( fbRestServer + fbRestNode + "?" + paramString );
if ( lines == null )
{
println( "OUCH, nothing to read from that URL:\n" + fbRestServer + fbRestNode + "?" + paramString );
return null;
}
String response = join( lines, "\n" );
return response;
}
/**
* Generate a call signature, see:
* http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application
* http://wiki.developers.facebook.com/index.php/Authorization_and_Authentication_for_Desktop_Applications
*/
String fbGenerateSIG ( String[] args )
{
java.util.Arrays.sort( args );
String argString = join( args, "" );
argString += fbApiSecret;
return md5Encode( argString );
}
/**
* MD5 encode a String using Processing API ( hex() )
*/
String md5Encode ( String data )
{
java.security.MessageDigest digest = null;
try {
digest = java.security.MessageDigest.getInstance("MD5");
}
catch ( java.security.NoSuchAlgorithmException nsae ) {
nsae.printStackTrace();
}
digest.update( data.getBytes() );
byte[] hash = digest.digest();
StringBuilder hexed = new StringBuilder();
for ( int i = 0; i < hash.length; i++ )
{
hexed.append( hex( hash[i], 2 ) );
}
return hexed.toString().toLowerCase();
}