Skip to content

Commit

Permalink
change __check methods to use pre-compiled regex
Browse files Browse the repository at this point in the history
  • Loading branch information
RobBrazier committed Mar 13, 2022
1 parent eb94de5 commit b47edb8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
9 changes: 5 additions & 4 deletions pypartpicker/regex.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import re

LIST_REGEX = re.compile("((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/(?:(?:list/(?:[a-zA-Z0-9]{6}))|(?:user/(?:[\\w]+)/saved/(?:[a-zA-Z0-9]{6}))))")
PRODUCT_REGEX = re.compile("((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/product/(?:[a-zA-Z0-9]{6}))")


def get_list_links(string):
list_regex = re.compile("((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/(?:(?:list/(?:[a-zA-Z0-9]{6}))|(?:user/(?:[\w]+)/saved/(?:[a-zA-Z0-9]{6}))))")
return re.findall(list_regex, string)
return re.findall(LIST_REGEX, string)


def get_product_links(string):
product_regex = re.compile("((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/product/(?:[a-zA-Z0-9]{6}))")
return re.findall(product_regex, string)
return re.findall(PRODUCT_REGEX, string)
5 changes: 3 additions & 2 deletions pypartpicker/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import re
import requests
from pypartpicker.regex import LIST_REGEX, PRODUCT_REGEX

from bs4 import BeautifulSoup
from functools import partial
Expand Down Expand Up @@ -89,12 +90,12 @@ def __make_soup(self, url) -> BeautifulSoup:
# Private Helper Function
# Uses a RegEx to check if the specified string matches the URL format of a valid PCPP parts list
def __check_list_url(self, url_str):
return re.search(r"((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/(?:(?:list/(?:[a-zA-Z0-9]{6}))|(?:user/(?:[\w]+)/saved/(?:[a-zA-Z0-9]{6}))))", url_str)
return re.search(LIST_REGEX, url_str)

# Private Helper Function
# Uses a RegEx to check if the specified string matches the URL format of a valid product on PCPP
def __check_product_url(self, url_str):
return re.search(r"((?:http|https)://(?:[a-z]{2}.)?pcpartpicker.com/product/(?:[a-zA-Z0-9]{6}))", url_str)
return re.search(PRODUCT_REGEX, url_str)

def fetch_list(self, list_url) -> PCPPList:
# Ensure a valid pcpartpicker parts list was passed to the function
Expand Down

0 comments on commit b47edb8

Please sign in to comment.