Commits

Burak Gürsoy committed 6a41379

Use Bitbucket REST API instead of the stupid and broken scraper.

  • Participants
  • Parent commits f77fdb5

Comments (0)

Files changed (2)

 these modules:
 
     HTML::TableParser
+    JSON
     LWP::UserAgent
     LWP::Protocol::https
     Software::License
 use strict;
 use warnings;
 use constant UA_TIMEOUT => 10;
-use constant NBSP       => chr 160;
 use LWP::UserAgent;
+use JSON;
 use Carp qw(croak);
 
-use constant BASE_URL => 'http://bitbucket.org/';
+use constant URL      => 'https://api.bitbucket.org/1.0/users/%s/';
+use constant BASE_URL => 'https://bitbucket.org/%s/%s/';
 
 our $VERSION = '0.10';
 
 
     if ( $r->is_success ) {
         my $raw = $r->decoded_content;
-        return $raw;
+        return JSON::from_json( $raw );
     }
 
     croak( 'GET request failed: ' . $r->as_string );
     my $self = shift;
     my $user = shift || croak 'No user name specified';
     warn ">> Fetching the base URL ...\n";
-    my $raw  = eval { $self->get( BASE_URL . $user ) };
+    my $raw  = eval { $self->get( sprintf URL, $user ) };
     croak "$user is not a valid user. Error: $@" if $@;
-    warn ">> Parsing ...\n";
-    my(undef, $rest)  = split m{\Q<ol id="repositories" class="detailed">\E}xms, $raw, 2;
-    my $rid = 0;
-    my @repo;
-    foreach my $line ( split m{\n+}xms, $rest ) {
-	chomp $line;
-	my $e = $repo[$rid] ||= {};
-	if ( $line =~ m{\s+ \Q<h3><a href="\E (.+?)/overview">(.+?)</a></h3>}xms ) {
-            if ( @repo ) {
-		$rid++;
-                $e = $repo[$rid] = {};
-	    }
-	    my($url, $name) = ($1, $2);
-	    $url       =~ s{\A / }{}xms;
-	    $e->{url}  = join q{}, BASE_URL, $url;
-	    $e->{name} = $name;
-	    next;
-	}
-
-        if ( $line =~ m{\Q<p class="metadata">Created <time datetime="\E(.+?)">(.+?)ago</time>}xms ) {
-	    $e->{ago} = $2;
-	    next;
-	}
-
-        if ( $line =~ m{\Q<div class="description"><p>\E(.+?)</p></div>}xms ) {
-	    $e->{desc} = $1;
-	    next;
-	}
-    }
-    shift @repo if ! keys %{ $repo[0] };
-    return @repo;
-}
-
-sub trim {
-    my $self = shift;
-    my $s = shift;
-    $s =~ s{ \A \s+ }{}xms;
-    $s =~ s{ \s+ \z }{}xms;
-    return $s;
+    croak "Data set is not a hash but $raw" if ref $raw ne 'HASH';
+    my $r = $raw->{repositories} || die "No 'repositories' key in resultset";
+    my @repos = sort { $a->{name} cmp $b->{name} }
+                map { {
+                    name => $_->{name},
+                    url  => sprintf( BASE_URL, $user, $_->{slug} ),
+                }}
+                @{ $r };
+    return @repos;
 }
 
 package main;