Skip to content

Commit 323171a

Browse files
authored
Merge pull request #13 from JakubDotPy/dev
removed hardcoded user name
2 parents fd9bcf3 + a0fad9a commit 323171a

File tree

348 files changed

+20
-17020
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

348 files changed

+20
-17020
lines changed

.idea/codewars_backup.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
**os.environ, # override loaded values with environment variables
1212
}
1313

14-
# urls
15-
USER_URL = fr"{ENV['API_URL']}users/{ENV['CW_USERNAME']}"
16-
1714
CURRENT_DIR = Path.cwd()
1815
LOGS_DIR = CURRENT_DIR / 'logs'
1916
OUTPUT_DIR = CURRENT_DIR / 'output'
@@ -23,7 +20,7 @@
2320
\"\"\"Kata - {name}
2421
2522
completed at: {completed_at:%Y-%m-%d %H:%M:%S}
26-
by: Jakub Červinka
23+
by: {completed_by}
2724
2825
{description}
2926
\"\"\"

main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import config
1919
from config import ENV
20-
from config import USER_URL
2120
from config import setup_logging
2221

2322
urllib3.disable_warnings()
@@ -37,6 +36,7 @@ class Kata:
3736
code: str = ''
3837
description: str = ''
3938
uploaded: bool = False
39+
completed_by: str = ''
4040

4141
def __post_init__(self):
4242
# parse
@@ -71,6 +71,7 @@ def __init__(self):
7171
self.completed_katas = []
7272
self.uploaded_katas = set()
7373
log.debug('Codewars agent prepared')
74+
self.username = self.get_username()
7475

7576
@staticmethod
7677
def _parse_cookie_env(raw_cookie: str) -> dict:
@@ -82,11 +83,22 @@ def _parse_cookie_env(raw_cookie: str) -> dict:
8283
for key, morsel in cookie.items()
8384
})
8485

85-
def get_user_data(self, url: str) -> dict:
86+
def get_username(self):
87+
"""Parse logged-in users' username."""
88+
log.debug('getting username')
89+
r = self.session.get('http://www.codewars.com/dashboard')
90+
user_image = r.html.find('.profile-pic img')[0]
91+
username = user_image.attrs['alt'].split()[0]
92+
return username
93+
94+
def get_user_data(self) -> dict:
8695
"""Fetch user info json from public API."""
8796
log.debug('getting user data')
97+
url = fr"{ENV['API_URL']}users/{self.username}"
8898
r = self.session.get(url, verify=False)
89-
return r.json()
99+
data = r.json()
100+
self.user_full_name = data['name']
101+
return data
90102

91103
@staticmethod
92104
def parse_user_info(user_data: dict) -> str:
@@ -103,7 +115,7 @@ def load_completed_katas_data(self):
103115
Save it internally for later use.
104116
"""
105117
log.debug('loading katas data')
106-
url = fr"{USER_URL}/code-challenges/completed"
118+
url = fr"{ENV['API_URL']}users/{self.username}/code-challenges/completed"
107119

108120
self.completed_katas = []
109121

@@ -152,6 +164,7 @@ def _parse_response(response: Response) -> tuple[str, str]:
152164

153165
def create_kata_obj(self, kata_data: dict) -> Kata:
154166
"""Create kata instance from api and parsed data."""
167+
kata_data['completed_by'] = self.user_full_name
155168
kata = Kata(**kata_data)
156169
kata.description, kata.code = self.get_kata_data(kata.id)
157170
return kata
@@ -174,7 +187,7 @@ def main():
174187
# prepare online agent
175188
cw = CodewarsAgent()
176189

177-
user_data = cw.get_user_data(USER_URL)
190+
user_data = cw.get_user_data()
178191
log.info(cw.parse_user_info(user_data))
179192

180193
# load completed katas info from public api

output/abbreviate_a_two_word_name.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

output/airport_arrivals_slash_departures_number_1.py

Lines changed: 0 additions & 96 deletions
This file was deleted.

output/all_star_code_challenge_number_15.py

Lines changed: 0 additions & 40 deletions
This file was deleted.

output/anagram_detection.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

output/are_they_the_same.py

Lines changed: 0 additions & 54 deletions
This file was deleted.

output/are_we_alternate.py

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)