Skip to content

Commit 866afbc

Browse files
committed
Delete old code, make requests respectful work locally
1 parent e7c7b2e commit 866afbc

File tree

3 files changed

+18
-96
lines changed

3 files changed

+18
-96
lines changed

datauploader/tasks.py

Lines changed: 2 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def process_github(oh_id):
3737
oh_access_token = oh_member.get_access_token(
3838
client_id=settings.OPENHUMANS_CLIENT_ID,
3939
client_secret=settings.OPENHUMANS_CLIENT_SECRET)
40-
github_data = get_existing_github_data(oh_access_token) # does it matter if we get existing?? updates may not be sequential. need to examine data
40+
github_data = get_existing_github_data(oh_access_token)#
4141
github_member = oh_member.datasourcemember
4242
github_access_token = github_member.get_access_token(
4343
client_id=settings.GITHUB_CLIENT_ID,
@@ -47,85 +47,7 @@ def process_github(oh_id):
4747

4848

4949
def update_github(oh_member, github_access_token, github_data):
50-
print(github_data)
51-
try:
52-
start_date_iso = arrow.get(get_start_date(github_data, github_access_token)).datetime.isocalendar()
53-
print(start_date_iso)
54-
print(type(start_date_iso))
55-
github_data = remove_partial_data(github_data, start_date_iso)
56-
stop_date_iso = (datetime.utcnow()
57-
+ timedelta(days=7)).isocalendar()
58-
# while start_date_iso != stop_date_iso:
59-
print(f'processing {oh_member.oh_id}-{oh_member.oh_id} for member {oh_member.oh_id}')
60-
print(github_access_token)
61-
query = TEST_QUERY
62-
response = graphql_query(github_access_token, query)
63-
github_data = response.json()
64-
print(github_data)
65-
print('successfully finished update for {}'.format(oh_member.oh_id))
66-
github_member = oh_member.datasourcemember
67-
github_member.last_updated = arrow.now().format()
68-
github_member.save()
69-
except RequestsRespectfulRateLimitedError:
70-
logger.debug(
71-
'requeued processing for {} with 60 secs delay'.format(
72-
oh_member.oh_id)
73-
)
74-
process_github.apply_async(args=[oh_member.oh_id], countdown=61)
75-
finally:
76-
replace_github(oh_member, github_data)
77-
78-
79-
80-
81-
def replace_github(oh_member, github_data):
82-
# delete old file and upload new to open humans
83-
tmp_directory = tempfile.mkdtemp()
84-
metadata = {
85-
'description':
86-
'Github activity feed, repository contents and stars data.',
87-
'tags': ['demo', 'Github', 'test'],
88-
'updated_at': str(datetime.utcnow()),
89-
}
90-
out_file = os.path.join(tmp_directory, 'github-data.json')
91-
logger.debug('deleted old file for {}'.format(oh_member.oh_id))
92-
api.delete_file(oh_member.access_token,
93-
oh_member.oh_id,
94-
file_basename="dummy-data.json")
95-
with open(out_file, 'w') as json_file:
96-
json.dump(github_data, json_file)
97-
json_file.flush()
98-
api.upload_aws(out_file, metadata,
99-
oh_member.access_token,
100-
project_member_id=oh_member.oh_id)
101-
logger.debug('uploaded new file for {}'.format(oh_member.oh_id))
102-
103-
104-
def remove_partial_data(github_data, start_date):
105-
return # FIXME: need to rethink this logic anyway
106-
remove_indexes = []
107-
for i, element in enumerate(github_data):
108-
element_date = datetime.strptime(
109-
element['date'], "%Y%m%d").isocalendar()[:2]
110-
if element_date == start_date:
111-
remove_indexes.append(i)
112-
for index in sorted(remove_indexes, reverse=True):
113-
del github_data[index]
114-
return github_data
115-
116-
117-
def get_start_date(github_data, github_access_token):
118-
if not github_data:
119-
url = GITHUB_API_BASE + "/user?access_token={}".format(
120-
github_access_token
121-
)
122-
response = rr.get(url, wait=True, realms=['github'])
123-
reso = response.json()
124-
print(reso)
125-
return reso['created_at']
126-
else:
127-
# FIXME
128-
return datetime.now()
50+
pass
12951

13052

13153
def get_existing_github_data(oh_access_token):

demotemplate/settings.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,22 @@
6868
GITHUB_REDIRECT_URI = os.getenv('GITHUB_REDIRECT_URI')
6969

7070
# Requests Respectful (rate limiting, waiting)
71-
if REMOTE is True:
72-
from urllib.parse import urlparse
73-
url_object = urlparse(os.getenv('REDIS_URL', 'redis://'))
74-
# logger.info('Connecting to redis at %s:%s',
75-
# url_object.hostname,
76-
# url_object.port)
77-
RespectfulRequester.configure(
78-
redis={
79-
"host": url_object.hostname,
80-
"port": url_object.port,
81-
"password": url_object.password,
82-
"database": 0
83-
},
84-
safety_threshold=5)
71+
from urllib.parse import urlparse
72+
url_object = urlparse(os.getenv('REDIS_URL', 'redis://127.0.0.1:6379'))
73+
print('Connecting to redis at %s:%s',
74+
url_object.hostname,
75+
url_object.port)
76+
RespectfulRequester.configure(
77+
redis={
78+
"host": url_object.hostname,
79+
"port": url_object.port,
80+
"password": url_object.password,
81+
"database": 0
82+
},
83+
safety_threshold=5)
8584

8685
# This creates a Realm called "github" that allows 150 requests per minute maximum.
86+
8787
rr = RespectfulRequester()
8888
rr.register_realm("github", max_requests=5000, timespan=3600)
8989

env.example

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SECRET_KEY='secret_key_here'
1111
# DEBUG='False'
1212

1313
# Redis configuration, default port is 6379
14-
REDIS_URL='redis://localhost:6379/1'
14+
REDIS_URL='redis://127.0.0.1:6379/1'
1515

1616
# Open Humans OAuth2 project settings.
1717
OH_ACTIVITY_PAGE='https://www.openhumans.org/activity/your-project-name-should-be-here/'
@@ -28,4 +28,4 @@ DJANGO_SETTINGS_MODULE='demotemplate.settings'
2828

2929
GITHUB_CLIENT_ID='foo'
3030
GITHUB_CLIENT_SECRET='bar'
31-
GITHUB_REDIRECT_URI='http://127.0.0.1:5000/moves_complete'
31+
GITHUB_REDIRECT_URI='http://127.0.0.1:5000/moves_complete'

0 commit comments

Comments
 (0)