下載 https://github.com/android 上的全部源代碼

從 https://android.googlesource.com/ 上弄源代碼下來真是不容易,不但得用 vpn,repo 還得半天。github 的速度就快多了,關鍵是不用 vpn,隨時可以下載。而且 github 還可以直接下載 zip 包,那速度可不是 repo 能比的。下面寫了個代碼批量下載 zip 包:


  1. #coding:cp936  
  2. import re, requests  
  3.   
  4. download_path = '.' # 壓縮包下載後的存放位置  
  5. tag = 'android-4.1.2_r2.1' # 分支或標籤的名稱,如果是主版本就寫 master  
  6.   
  7. base_url = 'https://github.com/android'  
  8. archive_url = 'https://github.com/android/%s/archive/%s.zip'  
  9. pagination_re = '<a href="/android\?page=.*?">(.*?)</a>'  
  10. repo_re = '<a href="/android/.*?" itemprop="name codeRepository">(.*?)</a>'  
  11. page_count = 1  
  12. repo_items = []  
  13.   
  14. session = requests.Session()  
  15. session.headers.update({  
  16.     'User-Agent''Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729)',  
  17. })  
  18.   
  19. html = session.get(base_url).text  
  20. page_result = re.findall(pagination_re, html, re.S)  
  21. if page_result != []:  
  22.     page_count = int(page_result[-2])  
  23.   
  24. repo_result = re.findall(repo_re, html, re.S)  
  25. if repo_result != []:  
  26.     repo_items += repo_result  
  27.   
  28. repo_items = map(lambda x: x.strip(), repo_items)  
  29.       
  30. if page_count > 1:  
  31.     current_page = 2  
  32.     while current_page <= page_count:  
  33.         html = session.get(base_url + "/?page=%d" % current_page).text  
  34.         repo_result = re.findall(repo_re, html, re.S)  
  35.         if repo_result != []:  
  36.             repo_items = repo_items + repo_result  
  37.         current_page += 1  

運行完後,會生成一個 bat 文件,裏面是用 wget 來下載的,結果如下。運行 bat 等待下載完成就行了。


  1. wget "https://github.com/android/platform_frameworks_base/archive/android-4.1.2_r2.1.zip" -c --output-document=".\platform_frameworks_base.zip" --no-check-certificate  
  2. wget "https://github.com/android/kernel_common/archive/android-4.1.2_r2.1.zip" -c --output-document=".\kernel_common.zip" --no-check-certificate 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章