הורדת עיקרים מ-Github פשוטה

GithubGist הוא מקום שבו אתה יכול ליצור תמצית פרטית או ציבורית, כלומר לאחסן את הקבצים שלך באופן פרטי או ציבורי. הבה נניח תרחיש שבו כתבת מספר לא מבוטל של עיקרים עבור הפרויקט שלך ואתה רוצה להוריד קבוצה מהם. הדרך היחידה שבה אתה יכול לעשות את זה באמצעות GithubGist זה לפתוח כל תמצית בודדת כדי להוריד ZIP או שיבוט דרך HTTP או SSH. מאמר זה עוסק בהפיכת המשימה לעיל לפשוטה יותר. באמצעות הפקודות שלמטה אתה יכול אפילו להוריד עיקרים ממשתמשי github אחרים למעט הפרטיים עד שתדע את הסיסמה שלהם. אנחנו נשתמש בקשות חבילה להצעה זו. זוהי חבילה מדהימה לשלוח בקשות HTTP עם קוד מינימלי. הַתקָנָה 1. הורד את החבילה מ PyPI באמצעות טרמינל באמצעות pip3 תַחבִּיר:
 pip3 install requests    פֶּתֶק:   To become a root user run the following command:  
 sudo pip3 install requests    סקריפט Python3 הסקריפט לא יכול לרוץ על IDE מקוון ולכן אתה יכול ללחוץ כָּאן   to see how it works. Python   
   import   requests   import   os   def   create_directory  (  dirname  ):   #Creates a new directory if a directory with dirname does not exist   try  :   os  .  stat  (  dirname  )   except  :   os  .  mkdir  (  dirname  )   def   show  (  obj  ):   #Displays the items in the obj   for   i   in   range  (  len  (  obj  )):   print  (  str  (  i  )  +  ': '  +  str  (  obj  [  i  ]))   def   auth  ():   #Asks for the user details   ask_auth   =   input  (  'Do you want to download gists from your account   ?   Type   'yes'   or   'no'  :   ')   if  (  ask_auth  ==  'yes'  ):   user   =   input  (  'Enter your username: '  )   password   =   input  (  'Enter your password: '  )   request   =   requests  .  get  (  'https://api.github.com/users/'  +  user  +  '/gists'      auth  =  (  user     password  ))   elif  (  ask_auth  ==  'no'  ):   user   =   input  (  'Enter username: '  )   request   =   requests  .  get  (  'https://api.github.com/users/'   +  user  +  '/gists'  )   return   [  ask_auth     user     request  ]   def   load  (  request  ):   #Loads the files and the gist urls   output   =   request  .  text  .  split  (  ''  )   gist_urls   =   []   files   =   []   for   item   in   output  :   if   'raw_url'   in   item  :   gist_urls  .  append  (  str  (  item  [  11  :  -  1  ]))   if   'filename'   in   item  :   files  .  append  (  str  (  item  .  split  (  ':'  )[  1  ][  2  :  -  1  ]))   return   [  gist_urls     files  ]   def   write_gist  (  filename     text  ):   #Writes text(gist) to filename   fp   =   open  (  filename     'w'  )   fp  .  write  (  text  )   fp  .  close  ()   def   download  (  permission     user     request     fileno  ):   #Loads and writes all the gists to   dirname     if  (  permission   ==   'yes'   or   permission   ==   'no'  ):   gist_urls     files   =   load  (  request  )   dirname   =   user  +  ''s_gists/'   create_directory  (  dirname  )   if  (  fileno  [  1  ]   ==   'all'  ):   for   i   in   range  (  len  (  gist_urls  )):   gist   =   requests  .  get  (  gist_urls  [  i  ])   write_gist  (  dirname  +  files  [  i  ]   gist  .  text  )   else  :   for   i   in   range  (  1    len  (  fileno  )):   gist   =   requests  .  get  (  gist_urls  [  int  (  fileno  [  i  ])])   write_gist  (  dirname  +  files  [  int  (  fileno  [  i  ])]   gist  .  text  )   def   detailed  (  urls     pos  ):   #Prints out the contents of a file   gist   =   requests  .  get  (  urls  [  int  (  pos  )])   print  (  gist  .  text  )   def   main  ():   #Authenticates and downloads gists according to user's choice   #Commands:   #show: To show all the available gists with their assigned gistno   #download all: To download all the available gists   #download gistno(s): To download gist(s) assigned to gistno(s)   #detailed gistno: To print content of gist assigned to gistno   #exit: To exit the script   ask_auth     user     request   =   auth  ()   urls     files   =   load  (  request  )   try  :   while  (  1  ):   command   =   input  (  'Enter your command: '  )   if  (  'download'   in   command  ):   download  (  ask_auth     user     request     command  .  split  (  ' '  ))   elif  (  'detailed'   in   command  ):   detailed  (  urls     command  .  split  (  ' '  )[  1  ])   elif  (  command   ==   'show'  ):   show  (  files  )   elif  (  command   ==   'exit'  ):   return   except  :   pass   if  (  __name__   ==   '__main__'  ):   main  ()   
הֶסבֵּר GithubGist API מאחסן מידע על כל משתמש בכתובת https://api.github.com/users/username/gists.
  • שלח בקשת HTTP לכתובת האתר שלמעלה כדי לאחזר מידע על המשתמש.
  • חפש את ה raw_url של כל תמצית זמינה ושלחו בקשות HTTP כדי לאחזר את המידע עליהם.
  • מניפולציה של המידע לצרכים שלך.