1. Brad Montgomery
  2. python-bitbucket

Commits

Brad Montgomery  committed a03e808

made forks() scraping a bit more robust

  • Participants
  • Parent commits 37e6264
  • Branches default

Comments (0)

Files changed (1)

File bitbucket/api.py

View file
         return json.loads(self.bb.load_url(url))
 
     def forks(self):
-        """ This is a dirty hack to get the number of Forks/Queues by scraping the src page """
+        """ 
+        This is a dirty hack to get the number of Forks/Queues by scraping the src page.
+        If the scraping fails, this just returns None
+        """
         url = 'https://bitbucket.org/%s/%s/src/' % (self.username, self.slug)
         response = urlopen(Request(url))
-        content = response.read()
-        # Dirty hack to find X in the first occurance of the string: "Forks/Queues (X)"
-        start_search = content.find('Forks/Queues')
-        open_paren_location = content.find('(', start_search)
-        close_paren_location = content.find(')', start_search)
-        num_forks = content[open_paren_location + 1:close_paren_location] # first part of the slice includes the paren
-        return int(num_forks)
+        if response.code == 200:
+            content = response.read()
+            # Dirty hack to find X in the first occurance of the string: "Forks/Queues (X)"
+            start_search = content.lower().find('forks/queues')
+            if start_search > 0:
+                open_paren_location = content.find('(', start_search)
+                close_paren_location = content.find(')', start_search)
+                num_forks = content[open_paren_location + 1:close_paren_location] # first part of the slice includes the paren
+                return int(num_forks)
+        return None
 
     def __repr__(self):
         return '<Repository: %s\'s %s>' % (self.username, self.slug)