Skip to content

removed hardcoded user name #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .idea/codewars_backup.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
**os.environ, # override loaded values with environment variables
}

# urls
USER_URL = fr"{ENV['API_URL']}users/{ENV['CW_USERNAME']}"

CURRENT_DIR = Path.cwd()
LOGS_DIR = CURRENT_DIR / 'logs'
OUTPUT_DIR = CURRENT_DIR / 'output'
Expand All @@ -23,7 +20,7 @@
\"\"\"Kata - {name}

completed at: {completed_at:%Y-%m-%d %H:%M:%S}
by: Jakub Červinka
by: {completed_by}

{description}
\"\"\"
Expand Down
23 changes: 18 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import config
from config import ENV
from config import USER_URL
from config import setup_logging

urllib3.disable_warnings()
Expand All @@ -37,6 +36,7 @@ class Kata:
code: str = ''
description: str = ''
uploaded: bool = False
completed_by: str = ''

def __post_init__(self):
# parse
Expand Down Expand Up @@ -71,6 +71,7 @@ def __init__(self):
self.completed_katas = []
self.uploaded_katas = set()
log.debug('Codewars agent prepared')
self.username = self.get_username()

@staticmethod
def _parse_cookie_env(raw_cookie: str) -> dict:
Expand All @@ -82,11 +83,22 @@ def _parse_cookie_env(raw_cookie: str) -> dict:
for key, morsel in cookie.items()
})

def get_user_data(self, url: str) -> dict:
def get_username(self):
"""Parse logged-in users' username."""
log.debug('getting username')
r = self.session.get('http://www.codewars.com/dashboard')
user_image = r.html.find('.profile-pic img')[0]
username = user_image.attrs['alt'].split()[0]
return username

def get_user_data(self) -> dict:
"""Fetch user info json from public API."""
log.debug('getting user data')
url = fr"{ENV['API_URL']}users/{self.username}"
r = self.session.get(url, verify=False)
return r.json()
data = r.json()
self.user_full_name = data['name']
return data

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

self.completed_katas = []

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

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

user_data = cw.get_user_data(USER_URL)
user_data = cw.get_user_data()
log.info(cw.parse_user_info(user_data))

# load completed katas info from public api
Expand Down
27 changes: 0 additions & 27 deletions output/abbreviate_a_two_word_name.py

This file was deleted.

96 changes: 0 additions & 96 deletions output/airport_arrivals_slash_departures_number_1.py

This file was deleted.

40 changes: 0 additions & 40 deletions output/all_star_code_challenge_number_15.py

This file was deleted.

23 changes: 0 additions & 23 deletions output/anagram_detection.py

This file was deleted.

54 changes: 0 additions & 54 deletions output/are_they_the_same.py

This file was deleted.

50 changes: 0 additions & 50 deletions output/are_we_alternate.py

This file was deleted.

Loading
Loading