Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Usage: download_issues.py owner repo postgres_dsn github_token
  4.  
  5. # simple script for downloading all issues from the given repository
  6. # requires you to set up an API token with no special permissions
  7. # also requires a PostgreSQL database with the following tables:
  8.  
  9. """
  10. create table issues (
  11. issue_number int unique not null,
  12. id bigint unique not null,
  13. state text,
  14. title text,
  15. body text,
  16. reporting_login text,
  17. created_at timestamptz,
  18. updated_at timestamptz,
  19. assignee text,
  20. raw jsonb
  21. );
  22.  
  23. create table comments (
  24. issue_number int references issues(issue_number),
  25. id bigint unique not null,
  26. commenting_login text,
  27. body text,
  28. updated_at timestamptz,
  29. raw jsonb
  30. );
  31. create index on comments(issue_number);
  32. """
  33.  
  34. import json
  35. import requests
  36. import github3
  37. import psycopg2
  38. import sys
  39.  
  40. if len(sys.argv) < 4:
  41. print "Usage:\n download_issues.py owner repo postgres_dsn github_token"
  42. sys.exit(1)
  43.  
  44. repo_owner = sys.argv[1]
  45. repo = sys.argv[2]
  46. pg_dsn = sys.argv[3]
  47. api_token = sys.argv[4]
  48.  
  49. # connect to database
  50. conn=psycopg2.connect(pg_dsn)
  51. cur=conn.cursor()
  52.  
  53. # connect to github
  54. gh = github3.login(token=api_token)
  55. issue_count=1
  56.  
  57. # loop through issues
  58. for issue_obj in gh.iter_repo_issues(repo_owner,repo,state='open'):
  59. # we're going to work with the raw json for most things
  60. # because the github3 lib leaves out several fields we need
  61. issue = issue_obj.to_json()
  62. # save to database
  63. if issue["assignee"]:
  64. asignee = issue["assignee"]["login"]
  65. try:
  66. cur.execute("""INSERT INTO issues
  67. VALUES ( %s, %s, %s, %s, %s, %s, %s, %s, %s, %s )""",
  68. (issue["number"], issue["id"], issue["state"],
  69. issue["title"], issue["body"], issue["user"]["login"],
  70. issue["created_at"], issue["updated_at"], asignee,
  71. json.dumps(issue),))
  72. except Exception as e:
  73. print 'inserting issue failed:'
  74. print json.dumps(issue, sort_keys=True, indent=2)
  75. print str(e)
  76. sys.exit(3)
  77. # get comments
  78. for comment_obj in issue_obj.iter_comments():
  79. # again, get JSON to get the fields the object doesn't have
  80. comment = comment_obj.to_json()
  81. # save comments to database
  82. try:
  83. cur.execute("""INSERT INTO comments
  84. VALUES (%s, %s, %s, %s, %s, %s)""",
  85. (issue["number"],comment["id"],comment["user"]["login"],
  86. comment["body"],comment["created_at"], json.dumps(comment),))
  87. except:
  88. print 'inserting comment failed:'
  89. print json.dumps(comment, sort_keys=True, indent=2)
  90. sys.exit(3)
  91. conn.commit()
  92. issue_count += 1
  93. if ( issue_count % 10 ) == 0:
  94. print "{0} issues downloaded".format(issue_count)
  95.  
  96. conn.close()
  97. print "DONE: {0} issues downloaded".format(issue_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement