8000 Add 2 more notebooks · etymancer/github3.py@671d4a1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 671d4a1

Browse files
committed
Add 2 more notebooks
1 parent 89a3480 commit 671d4a1

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:2d234b400db23a8a564289d081cd262369729acb74813177cd991585e0f66fe9"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "heading",
13+
"level": 1,
14+
"metadata": {},
15+
"source": [
16+
"Listing repositories by their owner"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"collapsed": false,
22+
"input": [
23+
"import github3"
24+
],
25+
"language": "python",
26+
"metadata": {},
27+
"outputs": [],
28+
"prompt_number": 1
29+
},
30+
{
31+
"cell_type": "code",
32+
"collapsed": false,
33+
"input": [
34+
"for repository in github3.repositories_by('sigmavirus24'):\n",
35+
" print('{0} created at {0.created_at}'.format(repository))"
36+
],
37+
"language": "python",
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"output_type": "stream",
42+
"stream": "stdout",
43+
"text": [
44+
"sigmavirus24/betamax created at 2013-07-16 03:23:22+00:00\n",
45+
"sigmavirus24/betamax_matchers created at 2014-07-19 18:51:39+00:00\n",
46+
"sigmavirus24/catapultpgh created at 2013-02-11 17:16:43+00:00\n",
47+
"sigmavirus24/charade created at 2012-11-28 03:24:20+00:00\n",
48+
"sigmavirus24/clint created at 2013-02-10 21:09:02+00:00\n",
49+
"sigmavirus24/contrib-hub created at 2013-11-30 18:36:55+00:00\n",
50+
"sigmavirus24/crequests created at 2013-01-24 22:54:17+00:00\n",
51+
"sigmavirus24/curryer created at 2013-11-10 02:03:42+00:00\n",
52+
"sigmavirus24/c_libs created at 2012-06-11 14:45:35+00:00\n",
53+
"sigmavirus24/ddg created at 2012-12-16 22:11:35+00:00\n",
54+
"sigmavirus24/developer.github.com created at 2012-05-27 04:19:17+00:00\n",
55+
"sigmavirus24/dream-python created at 2014-08-13 15:55:44+00:00\n",
56+
"sigmavirus24/elephant created at 2013-03-27 18:25:03+00:00\n",
57+
"sigmavirus24/euler.hs created at 2013-10-19 01:19:03+00:00\n",
58+
"sigmavirus24/expecter created at 2012-07-25 03:02:58+00:00\n",
59+
"sigmavirus24/format-geojson.clj created at 2013-10-25 22:17:20+00:00\n",
60+
"sigmavirus24/ghsync created at 2012-10-17 13:49:42+00:00\n",
61+
"sigmavirus24/git-hub created at 2012-10-05 19:35:45+00:00\n",
62+
"sigmavirus24/GITenberg created at 2012-09-25 21:07:30+00:00\n",
63+
"sigmavirus24/github-cli created at 2012-07-05 14:58:47+00:00\n",
64+
"sigmavirus24/github3.py created at 2012-03-13 19:58:53+00:00\n",
65+
"sigmavirus24/GitPython created at 2012-11-28 16:27:22+00:00\n",
66+
"sigmavirus24/grequests created at 2012-12-30 23:04:12+00:00\n",
67+
"sigmavirus24/gtk-vikb created at 2012-07-16 23:18:15+00:00\n",
68+
"sigmavirus24/heroku-buildpack-python created at 2013-01-19 18:09:35+00:00\n",
69+
"sigmavirus24/http11 created at 2014-07-21 01:57:47+00:00\n",
70+
"sigmavirus24/httpbin created at 2013-07-19 18:53:30+00:00\n",
71+
"sigmavirus24/hyper created at 2014-02-20 16:02:48+00:00\n",
72+
"sigmavirus24/issues.py created at 2012-02-25 21:32:31+00:00\n",
73+
"sigmavirus24/LendingClubAutoInvestor created at 2013-05-08 14:56:33+00:00\n",
74+
"sigmavirus24/libyaml created at 2013-12-01 18:27:17+00:00\n",
75+
"sigmavirus24/lyahfgg created at 2013-07-31 02:47:36+00:00\n",
76+
"sigmavirus24/madison_geojson created at 2013-10-25 20:40:41+00:00\n",
77+
"sigmavirus24/mccabe created at 2014-04-18 18:30:26+00:00\n",
78+
"sigmavirus24/mixfaster created at 2012-06-30 00:13:21+00:00\n",
79+
"sigmavirus24/mixminion created at 2011-09-06 13:34:15+00:00\n",
80+
"sigmavirus24/mock_github_api created at 2012-12-21 23:14:08+00:00\n",
81+
"sigmavirus24/nothub created at 2013-01-29 18:00:15+00:00\n",
82+
"sigmavirus24/No_Agenda_Bat_Sig created at 2010-11-05 03:18:10+00:00\n",
83+
"sigmavirus24/octokit.rb created at 2013-12-12 02:32:30+00:00\n",
84+
"sigmavirus24/ordinary created at 2013-06-15 18:09:48+00:00\n",
85+
"sigmavirus24/patch-converter created at 2012-11-26 13:52:57+00:00\n",
86+
"sigmavirus24/pelican created at 2012-06-14 14:25:45+00:00\n",
87+
"sigmavirus24/pelican-themes created at 2012-06-14 13:51:35+00:00\n",
88+
"sigmavirus24/pep8 created at 2012-09-06 14:13:14+00:00\n",
89+
"sigmavirus24/pip created at 2014-01-07 14:27:24+00:00\n",
90+
"sigmavirus24/polynomials.py created at 2013-04-10 03:12:06+00:00\n",
91+
"sigmavirus24/PyF-ck created at 2013-05-01 22:14:11+00:00\n",
92+
"sigmavirus24/pyflakes created at 2014-07-23 15:06:17+00:00\n",
93+
"sigmavirus24/pyflakes-old created at 2013-01-28 00:25:27+00:00\n",
94+
"sigmavirus24/python-guide created at 2012-06-15 18:00:03+00:00\n",
95+
"sigmavirus24/python-interview-questions created at 2014-08-13 02:32:38+00:00\n",
96+
"sigmavirus24/pyyaml created at 2013-12-01 18:29:02+00:00\n",
97+
"sigmavirus24/rack created at 2014-07-13 00:51:30+00:00\n",
98+
"sigmavirus24/recipes created at 2013-01-25 04:31:44+00:00\n",
99+
"sigmavirus24/repl_fun created at 2013-03-30 15:13:42+00:00\n",
100+
"sigmavirus24/requests created at 2012-06-15 13:47:06+00:00\n",
101+
"sigmavirus24/requests-data-schemes created at 2013-01-07 04:49:50+00:00\n",
102+
"sigmavirus24/requests-ntlm created at 2014-07-18 21:32:31+00:00\n",
103+
"sigmavirus24/requests-toolbelt created at 2013-12-29 21:19:08+00:00\n",
104+
"sigmavirus24/requests.rb created at 2013-02-01 02:52:28+00:00\n",
105+
"sigmavirus24/rfc3986 created at 2014-06-25 14:25:11+00:00\n",
106+
"sigmavirus24/shipit created at 2013-05-22 19:32:18+00:00\n",
107+
"sigmavirus24/solarized-pygment created at 2013-01-13 23:36:54+00:00\n",
108+
"sigmavirus24/sprunge.py created at 2012-06-09 14:19:26+00:00\n",
109+
"sigmavirus24/subscribed created at 2012-10-07 03:40:31+00:00\n",
110+
"sigmavirus24/Todo.txt-python created at 2012-09-16 19:31:58+00:00\n",
111+
"sigmavirus24/update_firefox created at 2012-06-19 01:56:02+00:00\n",
112+
"sigmavirus24/uritemplate created at 2013-05-14 16:52:08+00:00\n",
113+
"sigmavirus24/urllib3 created at 2013-12-04 03:04:13+00:00\n",
114+
"sigmavirus24/vcr created at 2013-09-18 03:15:42+00:00\n",
115+
"sigmavirus24/vim-stopsign created at 2013-09-03 13:26:03+00:00\n",
116+
"sigmavirus24/www.gittip.com created at 2013-01-16 13:15:38+00:00\n",
117+
"sigmavirus24/x11-ssh-askpass created at 2011-09-08 15:25:05+00:00\n",
118+
"sigmavirus24/zero created at 2013-09-15 02:17:30+00:00\n",
119+
"sigmavirus24/zero_buffer created at 2014-07-31 18:46:36+00:00\n"
120+
]
121+
}
122+
],
123+
"prompt_number": 2
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"If you wanted to, you could also login and fork each one of these repositories."
130+
]
131+
},
132+
{
133+
"cell_type": "code",
134+
"collapsed": false,
135+
"input": [
136+
"gh = github3.login('username', 'password')"
137+
],
138+
"language": "python",
139+
"metadata": {},
140+
"outputs": [],
141+
"prompt_number": 3
142+
},
143+
{
144+
"cell_type": "code",
145+
"collapsed": false,
146+
"input": [
147+
"for repository in gh.repositories_by('sigmavirus24'):\n",
148+
" repository.create_fork()"
149+
],
150+
"language": "python",
151+
"metadata": {},
152+
"outputs": []
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"Also if you had an organization you wanted to fork them to you could simply do"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"collapsed": false,
164+
"input": [
165+
"for repository in gh.repositories_by('sigmavirus24'):\n",
166+
" repository.create_fork('my-organization')"
167+
],
168+
"language": "python",
169+
"metadata": {},
170+
"outputs": []
171+
}
172+
],
173+
"metadata": {}
174+
}
175+
]
176+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:d38420ae020c12c6526e5f233904ef68f88783f31ada12a2a091b4d23240e68f"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "heading",
13+
"level": 1,
14+
"metadata": {},
15+
"source": [
16+
"Using GitHub's API with Two-Factor Authentication (2FA) Enabled"
17+
]
18+
},
19+
{
20+
"cell_type": "markdown",
21+
"metadata": {},
22+
"source": [
23+
"When you enable 2FA in GitHub, this setting carries over to your usage of the API. github3.py provides a simple interface to manage 2FA.\n",
24+
"\n",
25+
"First, you need to create a callback handler that will prompt the user for their token (which they will receive either by SMS or by checking a phone application)."
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"collapsed": false,
31+
"input": [
32+
"def callback_handler():\n",
33+
" prompt_str = 'Please enter your 2FA token now: '\n",
34+
" try:\n",
35+
" token = raw_input(prompt_str)\n",
36+
" except NameError:\n",
37+
" token = input(prompt_str)\n",
38+
" return token"
39+
],
40+
"language": "python",
41+
"metadata": {},
42+
"outputs": [],
43+
"prompt_number": 1
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"This handler will work on Python 2 and Python 3. (In Python 2, to prompt the user, you want to use `raw_input` but that was replaced in Python 3 with `input`.)\n",
50+
"\n",
51+
"This will simply ask the user to type in their token and then return it to github3.py. Naturally if you're developing a GUI, you want to write a different handler, but that is out of the scope of this example.\n",
52+
"\n",
53+
"To use the handler above you can pass it to the `github3.login` function."
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"collapsed": false,
59+
"input": [
60+
"import github3"
61+
],
62+
"language": "python",
63+
"metadata": {},
64+
"outputs": [],
65+
"prompt_number": 3
66+
},
67+
{
68+
"cell_type": "code",
69+
"collapsed": false,
70+
"input": [
71+
"gh = github3.login('username', 'password', two_factor_callback=callback_handler)"
72+
],
73+
"language": "python",
74+
"metadata": {},
75+
"outputs": [],
76+
"prompt_number": 4
77+
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"The beauty of using the callback means that it only is used when it is necessary. If you have 2FA turned off, you'll never be prompted.\n",
83+
"\n",
84+
"If you have 2FA enabled, then every call you make to the API will require you to provide a passcode. The tokens cannot be reliably cached by github3.py. To avoid this, the API documentation and github3.py suggest you create an API Token. You should decide which [scopes](https://developer.github.com/v3/oauth/#scopes) you want the token to have before creating."
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"collapsed": false,
90+
"input": [
91+
"authorization = github3.authorize('username', 'password', ['user'],\n",
92+
" two_factor_callback=callback_handler)\n",
93+
"gh = github3.login(token=authorization.token)"
94+
],
95+
"language": "python",
96+
"metadata": {},
97+
"outputs": []
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"metadata": {},
102+
"source": [
103+
"The call to `authorize` will create an `Authorization` object which has a token. With that you can pass the token to `login` and not have to worry about having to supply tokens generated by an mobile application or sent to your phone."
104+
]
105+
}
106+
],
107+
"metadata": {}
108+
}
109+
]
110+
}

0 commit comments

Comments
 (0)
0