๋ด ์ง๋ฌธ์ ์ totalCount๋ฅผ ์ง์ ํธ์ถํ๋ฉด None์ด ๋ฐํ๋์ง๋ง iterate๋ ์ซ์๋ฅผ ์ป์ ์ ์๋ค๋ ๊ฒ์ ๋๋ค.
repo_commits = repo.get_commits()
repo_total = 0
for _ in repo_commits:
repo_total = repo_total + 1
repo_total = repo.get_commits().totalCount
๋ด ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ์ต๋๋ค
import json
import os
import github
from github import Github
# First create a Github instance:
g = Github("grapebaba", "heatonn1",per_page=1000)
def main():
'''
Use small data for this application
:return:
'''
with open(os.path.join(os.path.expanduser("~"),'recruitbot_data.txt'),'w') as f:
for user in g.search_users("type:user")[0:10000]:
user_dict = {}
user_dict['username'] = user.login
user_dict['id'] = user.id
user_dict['profile_url'] = user.html_url
user_dict['location'] = user.location
user_dict['followers'] = user.followers
user_dict['private_gists'] = user.private_gists
user_dict['public_gists'] = user.public_gists
user_dict['name'] = user.name
user_dict['company'] = user.company
user_dict['blog_url'] = user.blog
user_dict['email'] = user.email
user_dict['id'] = user.id
user_dict['contributions']={}
for repo in user.get_watched():
try:
repo_total = 0
for _ in repo.get_commits():
repo_total = repo_total + 1
if repo.get_stats_contributors() is not None:
for contributor in repo.get_stats_contributors():
if contributor is not None and contributor.author.id == user_dict['id']:
user_dict['contributions'][repo.name]={}
user_dict['contributions'][repo.name]['contributor_commits']=contributor.total
user_dict['contributions'][repo.name]['repo_commits']=repo_total
user_dict['contributions'][repo.name]['language']=repo.language
user_dict['contributions'][repo.name]['stars']=repo.stargazers_count
print user_dict
break
except github.GithubException as e:
print e
f.write(json.dumps(user_dict)+"\n")
if __name__ == '__main__':
main()
๋ค๋ฅธ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋๋๋ก SSL ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
Traceback (most recent call last):
File "/tmp/collector.py", line 54, in <module>
main()
File "/tmp/collector.py", line 36, in main
for _ in repo.get_commits():
File "/usr/local/lib/python2.7/dist-packages/github/PaginatedList.py", line 48, in __iter__
newElements = self._grow()
File "/usr/local/lib/python2.7/dist-packages/github/PaginatedList.py", line 60, in _grow
newElements = self._fetchNextPage()
File "/usr/local/lib/python2.7/dist-packages/github/PaginatedList.py", line 161, in _fetchNextPage
headers=self.__headers
File "/usr/local/lib/python2.7/dist-packages/github/Requester.py", line 171, in requestJsonAndCheck
return self.__check(*self.requestJson(verb, url, parameters, headers, input, cnx))
File "/usr/local/lib/python2.7/dist-packages/github/Requester.py", line 212, in requestJson
return self.__requestEncode(cnx, verb, url, parameters, headers, input, encode)
File "/usr/local/lib/python2.7/dist-packages/github/Requester.py", line 251, in __requestEncode
status, responseHeaders, output = self.__requestRaw(cnx, verb, url, requestHeaders, encoded_input)
File "/usr/local/lib/python2.7/dist-packages/github/Requester.py", line 281, in __requestRaw
output = response.read()
File "/usr/lib/python2.7/httplib.py", line 557, in read
s = self._safe_read(self.length)
File "/usr/lib/python2.7/httplib.py", line 664, in _safe_read
chunk = self.fp.read(min(amt, MAXAMOUNT))
File "/usr/lib/python2.7/socket.py", line 380, in read
data = self._sock.recv(left)
File "/usr/lib/python2.7/ssl.py", line 341, in recv
return self.read(buflen)
File "/usr/lib/python2.7/ssl.py", line 260, in read
return self._sslobj.read(len)
์๋
,
totalCount๊ฐ None์ด๋ผ๋ ๊ฒ์ ๋ํด repo.get_pulls().totalCount์ ๋์ผํฉ๋๋ค.
๋ฌธ์ ๋ ๋ฐํ๋ JSON์ data['total_count']๊ฐ ํฌํจ๋์ด ์์ง ์๋ค๋ ๊ฒ์
๋๋ค.
PaginatedList๋ ๋ค์์ ์ฌ์ฉํ์ฌ __len__
๋ฅผ ๊ตฌํํด์ผ ํฉ๋๋ค.
if self.__totalCount:
return self.__totalCount
else:
return len(self.__elements)
@pgmillon ๊ณต์ ํ๊ฒ ๋ค๋ฆฝ๋๋ค.
๊ทธ๋ ๊ฒ ํ๋ ์์ PR ์ด์์ ์์ฑํ๊ณ ์ถ๋ค๋ฉด ๊ธฐ๊บผ์ด ๋ณํฉํ๊ฒ ์ต๋๋ค.
PaginatedList๋ ๊ฒฐ์ฝ totalCount๋ฅผ ์ค์ ํ์ง ์์ต๋๋ค. ์์์ len์ ๋ฐํํ๋ ๊ฒ์ ๋ด๊ฐ ์ฐพ๊ณ ์๋ ๊ฒ์ด ์๋๋๋ค. ์ค์ ๋ก ๊ฐ์ ธ์ฌ ์ ์๋ ํญ๋ชฉ์ด ๋ช ๊ฐ๋ ์๋์ง ์๊ณ ์ถ์ต๋๋ค.
๋์ํ์ง๋ง AFAIK API๋ ๊ทธ๊ฒ์ ์ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ์ง ์์ต๋๋ค. ๋ฐ๋ผ์ ์ ์ผํ ๋ฐฉ๋ฒ์ ํ์ฌ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋๋ค.
opened_pulls = repository.get_pulls()
pulls_count = 0
# Fix no count available on pulls list
for _ in opened_pulls:
pulls_count += 1
์์ :
from github import Github
gh = Github()
gists = gh.get_user('gil9red').get_gists()
print(gists.totalCount) # None
print(len(list(gists))) # 7
์ฌ์ ํ ๊ฐ์ ๋ฌธ์ :
from github import Github
g = Github()
repos = g.get_repos()
print(repos.totalCount) # None
@gil9red @Tigralt PaginatedList๋ฅผ ๋ฐ๋ณตํ์ฌ ๋ฐํ๋ ์ด ํญ๋ชฉ ์๋ฅผ ์ป์ผ๋ ค๊ณ ํฉ๋๊น? ๊ทธ๋ ๋ค๋ฉด PaginatedList๋ฅผ ๋ฐ๋ณตํ๊ณ ์นด์ดํธ๋ฅผ ์ฆ๊ฐ์ํค์ง ์๊ณ ๋ ์ด๋ฅผ ์ํํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค. ์ฌ๊ธฐ์์ ๋ฌธ์๋ฅผ ์ฐธ์กฐ
GitHub๋ ํ์ด์ง ๋งค๊น ์์ฒญ์ด ๋ฐํํ ํญ๋ชฉ ์๋ฅผ ์ ์ ์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ์ง ์์ผ๋ฏ๋ก PaginatedList์๋ ๊ธธ์ด๊ฐ ์์ต๋๋ค.
...
PaginatedList์ ๊ธธ์ด๋ฅผ ์ค์ ๋ก ์ทจํ๋ ค๋ ๊ฒฝ์ฐ ๋ช ์์ ์ผ๋ก [์๋ฌธ ๊ทธ๋๋ก] ๋ชฉ๋ก์ ๊ตฌ์ฑํ ๋ค์ ํด๋น ๊ธธ์ด๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค.
PaginatedList์ ํ ํ์ด์ง์ ์๋ ํญ๋ชฉ ์๋ฅผ ์ป์ผ๋ ค๋ ๊ฒฝ์ฐ ์ด PR ์ด ์์ง ์ด๋ ค ์๊ณ ๋ ผ์ ์ค์ธ ๊ฒ ๊ฐ์ต๋๋ค.
๋ช ๊ฐ์ง ๊ด๋ จ ๋ฌธ์ ์ดํด๋ณด๊ธฐ #433 #487 #596
total_count
์ด ์ด์ Github API ์๋ต์์ ์จ ๊ฒ ๊ฐ์๋ฐ์? ์ฐ๋ฆฌ๋ ์ ๊ฑฐํด์ผ TotalCount
์ ์์ฑ PaginatedList
๊ตฌํ๊ฐ ํ์๋๊ธฐ ๋๋ฌธ์ ํญ์ ๋ฐํ None
. ๋์ ๋ค์๊ณผ ๊ฐ์ ๋ฌธ์๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค.
# To get the total number of available elements in PaginatedList
repos = g.get_user().get_repos()
print(len(list(repos))) # we can't avoid to iterate through the whole set to get total count
๊ทธ๋ฆฌ๊ณ ํ์ฌ ์์ ์๋ฅผ ๋ฐํํ๊ธฐ ์ํด __len__
๋ฅผ ๊ตฌํํ ์ ์์ต๋๊น?
Github API๋ ์ฟผ๋ฆฌ์ ๋ํ ์ด ํ์ด์ง ์๋ฅผ ๋ฐํํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํ๋ฏ๋ก per_page=1
๋ฅผ ์ค์ ํ๋ฉด ๋จ์ผ ์์ฒญ์์ ์ด ํญ๋ชฉ ์๋ฅผ ์ป์ ์ ์์ด์ผ ํฉ๋๋ค. ์ด๊ฒ์ ๋ฐ๋ณต(ํฐ ๊ฒฐ๊ณผ ์งํฉ์ ๊ฒฝ์ฐ)๋ณด๋ค ํจ์ฌ ๋ ํจ์จ์ ์ผ ์ ์์ต๋๋ค.
@Tommos0 ์๊ฒ ์ต๋๋ค . ์๋ง๋ ์ข์ ์๊ฐ์ผ ๊ฒ์
๋๋ค. Link
ํค๋๋ฅผ ๊ฒ์ํ๊ธฐ ์ํด GET
๋์ HEAD
๋ฅผ ์ํํ ์๋ ์์ต๋๋ค.
์ด ๋ฌธ์ ๋ ์ต๊ทผ ํ๋์ด ์์๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค๋๋ ๊ฒ์ผ๋ก ํ์๋์์ต๋๋ค. ๋ ์ด์์ ํ๋์ด ์์ผ๋ฉด ํ์๋ฉ๋๋ค. ๊ทํ์ ๊ธฐ์ฌ์ ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
#820์์ ์์ ๋จ
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์์ :