Want more features on Pastebin? Sign Up, it's FREE!
Guest

Number of total BitBucket repositories, non-forks and forks

By: a guest on Oct 13th, 2011  |  syntax: Lisp  |  size: 2.34 KB  |  views: 51  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ;;; This counts projects and forks from BitBucket.org.
  2. ;;; It starts with the repositories list sorted by forks.
  3. ;;; Then, it scraps for the count of forks, and it sums it,
  4. ;;;plus 1 for the original repository.
  5. ;;; When it finds a repository with zero forks, it sets a flag
  6. ;;;so that it calculates the remaining from the number of
  7. ;;;repositories per page.
  8. ;;; It actually calculates the remaining pages but the last, so
  9. ;;;the last page is fetched and scrapped for a more accurate sum.
  10.  
  11. (require 'cl)
  12.  
  13. (with-temp-buffer
  14.   ;; Fetch non-fork repositories from bitbucket.org (set the integer to the number of pages)
  15.   (let ((all-repositories 0)
  16.         (non-forks 0)
  17.         (repos-per-page 0)
  18.         (pages 0)
  19.         (no-more-p nil))
  20.     (erase-buffer)
  21.     (shell-command (concat "wget -qO- --no-check-certificate \"" "https://bitbucket.org/repo/all/forks/" "\"") t)
  22.     (goto-char (1+ (buffer-size)))
  23.     (if (re-search-backward "<p class=\"subtitle\">\\(.*?\\) repositories</p>")
  24.         ;; The count has commas to separate each 3 digits
  25.         (setq non-forks (car (read-from-string (replace-regexp-in-string "," "" (match-string 1)))))
  26.       (error "Could not parse a total from the search page."))
  27.     (goto-char 1)
  28.     (while (re-search-forward "<a href=\".*?\" title=\"View forks\">\\(.*?\\)</a>" nil t)
  29.       (incf repos-per-page))
  30.     ;; Assume we have more than one page
  31.     (setq pages (ceiling non-forks repos-per-page))
  32.     (dotimes (i pages)
  33.       (when no-more-p
  34.         (message "Skipping pages with 0 forms")
  35.         (incf all-repositories (* (- pages no-more-p 1) repos-per-page))
  36.         (setq i (1- pages)))
  37.       (message "Fetching and processing page %d of %d, current count %d..." (1+ i) pages all-repositories)
  38.       ;; In the case of the first page, it's been fetched already
  39.       (unless (zerop i)
  40.         (erase-buffer)
  41.         (let ((http-url (concat "https://bitbucket.org/repo/all/forks/" (number-to-string (1+ i)) "/")))
  42.           (shell-command (concat "wget -qO- --no-check-certificate \"" http-url "\"") t)))
  43.       (goto-char 1)
  44.       (while (re-search-forward "<a href=\".*?\" title=\"View forks\">\\(.*?\\)</a>" nil t)
  45.         (let ((forks (car (read-from-string (match-string 1)))))
  46.           (when (zerop forks)
  47.             (setq no-more-p (1+ i)))
  48.           (incf all-repositories (1+ forks)))))
  49.     (message "Total sum of public non-fork and fork repositories is %d." all-repositories)
  50.     all-repositories))
  51.  
  52.  
clone this paste RAW Paste Data