-
Notifications
You must be signed in to change notification settings - Fork 276
/
example_18_04_manualparsing.pde
61 lines (51 loc) · 1.69 KB
/
example_18_04_manualparsing.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
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com
// Example 18-5: Parsing IMDB manually
String name = "Mary Poppins";
String runningtime;
PImage poster;
void setup() {
size(300, 350);
loadData();
}
void draw() {
background(255);
// Display all the stuff we want to display
image(poster, 10, 10, 164, 250);
fill(0);
text(name, 10, 300);
text(runningtime, 10, 320);
}
// Grab the data from IMDB
void loadData() {
// Get the raw HTML source into an array of strings
// (each line is one element in the array)
String url = "http://www.imdb.com/title/tt0058331/";
String[] lines = loadStrings(url);
// Turn array into one long String
String html = join(lines, "");
// Searching for running time
String start = "<time itemprop=\"duration\" datetime=\"PT139M\">";
String end = "</time>";
runningtime = giveMeTextBetween(html, start, end);
// Searching for poster image
start = "<link rel='image_src' href=\"";
end = "\">";
String imgUrl = giveMeTextBetween(html, start, end);
poster = loadImage(imgUrl);
}
// A function that returns a substring between two substrings
String giveMeTextBetween(String s, String before, String after) {
String found = "";
int start = s.indexOf(before); // Find the index of the beginning tag
if (start == -1) {
return ""; // If we don't find anything, send back a blank String
}
start += before.length(); // Move to the end of the beginning tag
int end = s.indexOf(after, start); // Find the index of the end tag
if (end == -1) {
return ""; // If we don't find the end tag, send back a blank String
}
return s.substring(start, end); // Return the text in between
}