public
Description: Convert BrightKite checkins to GPX format
Homepage: http://www.zenwheel.com/projects/software.html
Clone URL: git://github.com/zenwheel/brightkitegpx.git
brightkitegpx / BrightKiteGPX.pl
100644 134 lines (107 sloc) 3.502 kb
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
#!/usr/bin/perl
 
use LWP::UserAgent;
use URI::URL;
use HTTP::Request;
 
die "usage: <brightkite user>\n" if($#ARGV < 0);
 
dbmopen(%db, $ENV{'HOME'} . '/.bkhistory.' . $ARGV[0], 0666);
my $count = keys %db;
warn "Loaded $count cached entries.\n";
 
my $geturl = "http://brightkite.com/people/$ARGV[0]/objects.rss?limit=100";
my $agent = 'Mozilla/4.61 [en] (Win98; I)';
 
sub GetUrl {
my ($geturl) = @_;
my $ua = new LWP::UserAgent;
my $request;
 
my $url = new URI::URL($geturl);
 
$request = new HTTP::Request('GET', $url);
 
$request->header('User-Agent', $agent);
 
my $response = $ua->request($request, undef, undef);
 
if($response->is_success) {
#my $str = $response->as_string;
my $str = $response->content();
return($str);
} else {
#if($debug) {
# print $response->code . "\n";
# my $str = $response->as_string;
# print $str . "\n";
#}
return("");
}
}
 
my $str = GetUrl($geturl);
die "Can't download data for $ARGV[0]\n" if($str eq "");
 
my $lat = 0;
my $long = 0;
my $timestamp = 0;
my $name = '';
my $comment = '';
 
my $skipped = 0;
my $newEntries = 0;
 
# Parse GeoFeed
 
my @lines = split(/[\r\n]+/, $str);
foreach my $line (@lines) {
$lat = $1 if($line =~/<geo:lat>([^<]*)/);
$long = $1 if($line =~ /<geo:long>([^<]*)/);
$timestamp = $1 if($line =~ /<bk:timestamp>([^<]*)/);
$name = $1 if($line =~ /<bk:placeName>([^<]*)/);
$comment = $1 if($line =~ /<guid>([^<]*)/);
if($line =~ /<\/item>/) {
if($comment ne '' && defined($db{$comment})) {
$skipped++;
$name = '';
$comment = '';
next;
}
 
my @timeData = gmtime($timestamp);
my $y = $timeData[5] + 1900;
my $mon = sprintf "%02d", $timeData[4] + 1;
my $d = sprintf "%02d", $timeData[3];
my $h = sprintf "%02d", $timeData[2];
my $m = sprintf "%02d", $timeData[1];
my $s = sprintf "%02d", $timeData[0];
 
my $entry = '';
$entry = "<trkpt lat=\"$lat\" lon=\"$long\">\n";
$entry .= "<time>$y-$mon-$d" . "T$h:$m:$s" . "Z</time>\n";
$entry .= "<name>$name</name>\n" if($name ne '');
$entry .= "<cmt>$comment</cmt>\n" if($comment ne '');
$entry .= "</trkpt>\n";
 
$db{$comment} = $entry if($comment ne '');
$newEntries++;
$name = '';
$comment = '';
}
}
 
warn "Loaded $newEntries new entries.\n";
warn "Skipped $skipped duplicate entries.\n";
 
# Create GPX file from cached entries
 
print("<?xml version=\"1.0\" standalone=\"yes\"?>\n");
print("<gpx version=\"1.0\" creator=\"BrightKiteGPX 1.0\"");
print(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
print(" xmlns=\"http://www.topografix.com/GPX/1/0\"");
print(" xmlns:topografix=\"http://www.topografix.com/GPX/Private/TopoGrafix/0/2\"");
print(" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/Private/TopoGrafix/0/2 http://www.topografix.com/GPX/Private/TopoGrafix/0/2/topografix.xsd\">\n");
my @timeData = gmtime(time);
my $y = $timeData[5] + 1900;
my $mon = sprintf "%02d", $timeData[4] + 1;
my $d = sprintf "%02d", $timeData[3];
my $h = sprintf "%02d", $timeData[2];
my $m = sprintf "%02d", $timeData[1];
my $s = sprintf "%02d", $timeData[0];
print "<time>$y-$mon-$dT$h:$m:$s" . "Z</time>\n";
 
print "<trk>\n";
print "<trkseg>\n";
 
# sort by time
my @gpx = values %db;
my @sorted = sort {
$a =~ /<time>([^<]*)/; my $first = $1;
$b =~ /<time>([^<]*)/; my $second = $1;
return $first cmp $second;
} @gpx;
foreach my $val (@sorted) {
print "$val";
}
 
print "</trkseg>\n";
print "</trk>\n";
print "</gpx>\n";
 
dbmclose(%db);