{"id":4292,"date":"2019-12-12T10:25:24","date_gmt":"2019-12-12T09:25:24","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=4292"},"modified":"2019-12-12T10:29:44","modified_gmt":"2019-12-12T09:29:44","slug":"github-with-python-aka-pygithub-module","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/github-with-python-aka-pygithub-module\/","title":{"rendered":"Github with Python aka pyGithub module"},"content":{"rendered":"<p>Hi! This time we are gonna explore the module pyGithub to see if we can manage our repositories easily using Python.<\/p>\n<h2>Install pygithub<\/h2>\n<p>To install pyGithub, as usual, use pip:<\/p>\n<p><strong>pip install pyGithub<\/strong><\/p>\n<h2>Access to your repositories<\/h2>\n<p>First of all, import the module like this<\/p>\n<pre class=\"lang:default decode:true \">from github mport Github<\/pre>\n<p>then you have to give your credentials (put your username and the password):<\/p>\n<pre class=\"lang:default decode:true \">g = Github(\"username\",\"password\")<\/pre>\n<p>Now, throug the &#8216;g&#8217; object (istance of Github) you can have access to the repositories.<\/p>\n<h2>Get informations about yourself<\/h2>\n<p>You can have your username and your login name like this:<\/p>\n<pre class=\"lang:default decode:true\">user = g.get_user()\r\nprint(user.name)\r\nprint(user.login)<\/pre>\n<h2>Listing your repositories<\/h2>\n<p>To get the list of repositories do this:<\/p>\n<pre class=\"lang:default decode:true \">repos = g.get_user().get_repos()\r\nfor repo in repos:\r\n print(repo.name)<\/pre>\n<h2>Get a specific repo<\/h2>\n<p>To get a specific repo:<\/p>\n<pre class=\"lang:default decode:true\"># use the name of your repository instead of \"formazione....\"\r\nrepo = g.get_user().get_repo(\"formazione.github.io\")\r\n\r\n# Output\r\n# &gt;&gt;&gt; repo\r\n# &gt;&gt;&gt; 'formazione.github.io'<\/pre>\n<h2>Get all the files in a repo<\/h2>\n<pre class=\"lang:default decode:true\">contents = repo.get_contents(\"\")\r\nfor c in contents:\r\n print(c.path)\r\n\r\n# you will get all your files list<\/pre>\n<h2>Get the stars&#8230;<\/h2>\n<p>To get the stars of a repo:<\/p>\n<pre class=\"lang:default decode:true \">repo.stargazers_count<\/pre>\n<h2>Get a specific content<\/h2>\n<p>To get a specific content<\/p>\n<pre class=\"lang:default decode:true \">content = repo.get_contents(\"index.html\")\r\n<\/pre>\n<h2>Create a new file in the repo<\/h2>\n<p>To create a new file in the repo:<\/p>\n<pre class=\"lang:default decode:true \">repo.create_file(\"text.txt\",\"created a text file\", \"This is the content of the file\")\r\n# this will go in the master... to use a brach add branch='...'<\/pre>\n<h2>Update the file you created<\/h2>\n<p>To update the file you created:<\/p>\n<pre class=\"lang:default decode:true \">contents = repo.get_contents(\"text.txt\")\r\nrepo.update_file(contents.path, \"adding stuffs\", \"This is the new content of the file text.txt\", contents.sha)<\/pre>\n<h2>The live coding video<\/h2>\n<p><iframe loading=\"lazy\" title=\"Use Python to access Github repositories\" width=\"747\" height=\"420\" src=\"https:\/\/www.youtube.com\/embed\/xfYDy4nSc28?feature=oembed&amp;enablejsapi=1\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n\n","protected":false},"excerpt":{"rendered":"See how you can look at your repositories on github, create new files and update them with Python at ease.\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/github-with-python-aka-pygithub-module\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":4293,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[644],"tags":[438,645,4],"class_list":["post-4292","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-github","tag-github","tag-pygithub","tag-python"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4292","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=4292"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4292\/revisions"}],"predecessor-version":[{"id":4295,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/4292\/revisions\/4295"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media\/4293"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=4292"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=4292"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=4292"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}