Skip to content

Commit

Permalink
Merge pull request #11 from CIAvash/fix_getting_league_urls_dynamically
Browse files Browse the repository at this point in the history
Fix getting league URLs dynamically
  • Loading branch information
md-arif-shaikh authored Jul 10, 2023
2 parents 86bb9b2 + 29e819e commit 36d22b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
43 changes: 15 additions & 28 deletions soccer-leagues.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
"Get the base url of the website to get data from."
"https://www.theguardian.com/football/")

(defun soccer-leagues--get-base-league-url (league)
"Get the base url for a LEAGUE."
(if (member league (mapcar #'car soccer-leagues--leagues-alist))
(cdr (assoc league soccer-leagues--leagues-alist))
(user-error "Unknown league %s" league)))

(defun soccer-leagues--get-club-names-and-urls (league)
"Get the team names and the corresponding urls for a LEAGUE."
(let* ((url (concat (soccer-leagues--get-base-league-url league) "/table")))
Expand All @@ -60,43 +66,24 @@

(defun soccer-leagues--get-competition-names-and-urls ()
"Get the competition names and the corresponding urls."
(let* ((url "https://www.theguardian.com/football/competitions"))
(let* ((base-url "https://www.theguardian.com") (url (concat base-url "/football/competitions")))
(with-current-buffer (url-retrieve-synchronously url)
(let* ((dom (libxml-parse-html-region (point-min) (point-max)))
(data-dom (dom-by-class dom "fc-item fc-item--list-compact"))
(url-list (cl-loop for d in data-dom
collect (cons (car (dom-strings d)) (dom-attr (dom-by-tag d 'a) 'href)))))
(sections (dom-elements (dom-by-id dom "maincontent") 'data-container-name "^nav/list$"))
(url-list (cl-loop for section in sections
append (cl-loop for item in (dom-by-tag section 'li)
for name = (car (dom-strings item))
for url = (concat base-url (dom-attr (dom-by-tag item 'a) 'href))
collect (cons name url)))))
url-list))))

(defgroup soccer-leagues nil
"Customization group for soccer leagues."
:group 'soccer
:link '(url-link :tag "Homepage" "https://github.com/md-arif-shaikh/soccer"))

;;; The functions to fetch league and urls is not working anymore. Hardcoding some of these.
;;;(setq soccer-leagues--leagues-alist (soccer-leagues--get-competition-names-and-urls))
(defcustom soccer-leagues-leagues-alist '(("Premier League" . "https://www.theguardian.com/football/premierleague")
("La Liga" . "https://www.theguardian.com/football/laligafootball")
("Serie A" . "https://www.theguardian.com/football/serieafootball")
("Bundeshliga" . "https://www.theguardian.com/football/bundesligafootball")
("Ligue 1" . "https://www.theguardian.com/football/ligue1football")
("Champions League" . "https://www.theguardian.com/football/championsleague")
("FA Cup" . "https://www.theguardian.com/football/fa-cup")
("Carabao Cup" . "https://www.theguardian.com/football/carabao-cup")
("Championship" . "https://www.theguardian.com/football/championship")
("Europa League" . "https://www.theguardian.com/football/uefa-europa-league")
("Conference League" . "https://www.theguardian.com/football/europa-conference-league")
("MLS" . "https://www.theguardian.com/football/mls")
("Women's Super League" . "https://www.theguardian.com/football/womens-super-league"))
"Alist of league and urls."
:type '(alist :value-type (group integer))
:group 'soccer)

(defun soccer-leagues--get-base-league-url (league)
"Get the base url for a LEAGUE."
(if (member league (mapcar #'car soccer-leagues-leagues-alist))
(cdr (assoc league soccer-leagues-leagues-alist))
(user-error "Unknown league %s" league)))
(defvar soccer-leagues--leagues-alist)
(setq soccer-leagues--leagues-alist (soccer-leagues--get-competition-names-and-urls))

(provide 'soccer-leagues)

Expand Down
6 changes: 3 additions & 3 deletions soccer.el
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@
:group 'soccer-face)

(defun soccer--get-league-names ()
"Extract league names from `soccer-leagues-leagues-alist'."
(mapcar 'car soccer-leagues-leagues-alist))
"Extract league names from `soccer-leagues--leagues-alist'."
(mapcar 'car soccer-leagues--leagues-alist))

(defun soccer--get-league-url (league)
"Get url of a LEAGUE."
Expand Down Expand Up @@ -513,7 +513,7 @@
(defun soccer--all-clubs ()
"Get all club names."
(delete-dups (-flatten (cl-loop for leagues in (soccer--league-names)
collect (mapcar 'car (cdr (assoc leagues soccer-leagues-leagues-alist)))))))
collect (mapcar 'car (cdr (assoc leagues soccer-leagues--leagues-alist)))))))

(defun soccer-scorecard (date home away)
"Get the socrecard for a match between HOME and AWAY on a DATE. Enter DATE in YYYY-MM-DD format if entering it manually. If the input is from `org-read-date' calendar popup then it is in YYYY-MM-DD format by default."
Expand Down

0 comments on commit 36d22b2

Please sign in to comment.