Skip to content

Commit

Permalink
Improve user check
Browse files Browse the repository at this point in the history
  • Loading branch information
apie committed Aug 1, 2024
1 parent 2540e3b commit ac0a537
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
11 changes: 10 additions & 1 deletion scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import base64
import json
import re
import requests
from lxml import html
from typing import Optional, Iterable, Dict, Tuple
Expand Down Expand Up @@ -205,8 +206,16 @@ def get_user_info(username):
return _get_user_info(username)


username_regex = re.compile(r'^[a-z][\w-]{1,14}$', flags=re.ASCII)


def username_exists(username):
if get_user_info(username):
''' From last.fm sign up page:
"Your username should be between 2 and 15 characters, begin with a letter and contain only letters, numbers, '_' or '-'"
Check username first locally for this format, and then remote for existence.
'''
username = username.lower()
if (username == 'last.hq' or re.match(username_regex, username)) and get_user_info(username):
return True


Expand Down
26 changes: 26 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import re

from scrape import username_regex


class TestUserName(unittest.TestCase):
def test_username_regex(self):
username_list = [
('a1', True),
('a1234567890', True),
('a1234567890-_', True),
('a12345678912345', True),
('a', False),
('a()', False),
('aa ', False),
('123', False),
('a123456789123456', False),
]
for username, valid in username_list:
with self.subTest(username=username):
self.assertEqual(re.match(username_regex, username) is not None, valid)


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion util.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_recent_users():
with open(RECENT_USERS_FILE) as f:
return f.read()
except FileNotFoundError:
return ""
return "last.hq"


def add_recent_user(username):
Expand Down
2 changes: 1 addition & 1 deletion utils/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_user_info(username):
print("Getting " + url.replace(API_KEY, 'SECRET'))
from scrape import TIMEOUT
resp = session.get(url, timeout=TIMEOUT)
if resp.status_code == 404:
if resp.status_code != 200:
return ''
# Dump json as text so we can cache it to disk
return resp.text

0 comments on commit ac0a537

Please sign in to comment.