Skip to content

Commit

Permalink
Merge pull request #11 from RobBrazier/fix-user-list-regex
Browse files Browse the repository at this point in the history
Fix user list regex
  • Loading branch information
thefakequake committed Mar 15, 2022
2 parents 8cb46d0 + b47edb8 commit 0c15dda
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 @@ -97,12 +98,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 0c15dda

Please sign in to comment.