Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
graph4stackoverflow
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Liam Byrne
graph4stackoverflow
Commits
8d4438ca
Commit
8d4438ca
authored
2 years ago
by
Liam Byrne
Browse files
Options
Downloads
Patches
Plain Diff
Stack API for getting user activity, moving to using dump data next
parent
9eb34c1e
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
data-collection/stack_exchange_api.py
+50
-2
50 additions, 2 deletions
data-collection/stack_exchange_api.py
with
50 additions
and
2 deletions
data-collection/stack_exchange_api.py
+
50
−
2
View file @
8d4438ca
from
stackapi
import
StackAPI
,
StackAPIError
from
stackapi
import
StackAPI
,
StackAPIError
import
logging
import
logging
from
typing
import
List
logging
.
basicConfig
(
level
=
logging
.
INFO
)
logging
.
basicConfig
(
level
=
logging
.
INFO
)
class
StackExchangeAPI
:
class
StackExchangeAPI
:
def
__init__
(
self
):
def
__init__
(
self
,
max_pages
:
int
,
page_size
:
int
):
try
:
try
:
self
.
_SITE
=
StackAPI
(
'
stackoverflow
'
)
self
.
_SITE
=
StackAPI
(
'
stackoverflow
'
)
self
.
_SITE
.
max_pages
=
max_pages
self
.
_SITE
.
page_size
=
page_size
except
StackAPIError
as
e
:
except
StackAPIError
as
e
:
logging
.
error
(
"
Error URL: {}
"
.
format
(
e
.
url
))
logging
.
error
(
"
Error URL: {}
"
.
format
(
e
.
url
))
logging
.
error
(
"
Error Code: {}
"
.
format
(
e
.
code
))
logging
.
error
(
"
Error Code: {}
"
.
format
(
e
.
code
))
logging
.
error
(
"
Error Error: {}
"
.
format
(
e
.
error
))
logging
.
error
(
"
Error Error: {}
"
.
format
(
e
.
error
))
logging
.
error
(
"
Error Message: {}
"
.
format
(
e
.
message
))
logging
.
error
(
"
Error Message: {}
"
.
format
(
e
.
message
))
"""
The following methods will be used for data collection in Iteration 1:
User-expertise graph
"""
def
get_user_ids
(
self
)
->
List
[
int
]:
"""
Request a set of users and return their ids.
:return: List of ids
"""
response
=
self
.
_SITE
.
fetch
(
'
users
'
,
min
=
1000
)
user_ids
=
[
user
[
'
account_id
'
]
for
user
in
response
[
'
items
'
]]
return
user_ids
def
get_user_activity
(
self
,
account_ids
:
List
[
int
]):
"""
Get questions, answers, comments asked by the users
:param account_ids: List of user ids
:return:
"""
if
len
(
account_ids
)
>
100
:
logging
.
error
(
"
get_user_activity: account_ids should not have more than 100 ids.
"
)
return
None
response_answers
=
self
.
_SITE
.
fetch
(
'
users/{ids}/answers
'
,
ids
=
account_ids
,
body
=
True
)
print
(
response_answers
)
response_questions
=
self
.
_SITE
.
fetch
(
'
users/{ids}/questions
'
,
ids
=
account_ids
,
body
=
True
)
print
(
response_questions
)
response_comments
=
self
.
_SITE
.
fetch
(
'
users/{ids}/comments
'
,
ids
=
account_ids
)
print
(
response_comments
)
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
a
=
StackExchangeAPI
()
a
=
StackExchangeAPI
(
1
,
100
)
ids
=
a
.
get_user_ids
()
print
(
len
(
ids
))
a
.
get_user_activity
(
ids
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment