-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add nuxt util to scrape formid from public libcal calendar page (…
…#515) * feat: add nuxt util and slug function * chore: update ucla-library-website-components * fix: uncomment code * feat: improvement on scrapeFormId * fix: url * feat: improvement on scrapeFormId * fix: scrapeFormId Fix with parinita * fix: scrapeFormId Fix with parinita * fix: use plugin instead of helper functions for scraping form id from third party API * fix: update component package version Co-authored-by: pghorpade <pghorpade@library.ucla.edu>
- Loading branch information
1 parent
7bab2fe
commit 5829229
Showing
6 changed files
with
720 additions
and
796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export default function({$axios}, inject){ | ||
const libcalURL = "https://calendar.library.ucla.edu/event/" | ||
inject('scrapeApi', { | ||
scrapeFormId, | ||
}) | ||
async function scrapeFormId(eventId = ""){ | ||
const response = await fetch(libcalURL + eventId) | ||
const html = await response.text() | ||
var parser = new DOMParser() | ||
var doc = parser.parseFromString(html, "text/html") | ||
let formId = doc.querySelector("input[name='fid']").getAttribute("value") | ||
console.log(formId) | ||
console.log(doc) | ||
const formData = await fetchformData(formId) | ||
return formData | ||
} | ||
|
||
async function fetchformData(formId){ | ||
const formData = await $axios.$get(`api/1.1/events/form/${formId}`) | ||
return formData | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/** | ||
* Scrapping function in Nuxt utils directory, this will accept event-id from event slug page, and return formId. | ||
* @param {String} eventId | ||
* @returns {String} | ||
*/ | ||
|
||
function scrapeFormId(eventId = "", axios) { | ||
fetch('https://calendar.library.ucla.edu/event/' + eventId) | ||
.then(function(response) { | ||
// When the page is loaded convert it to text | ||
return response.text() | ||
}) | ||
.then(function(html) { | ||
// Initialize the DOM parser | ||
var parser = new DOMParser() | ||
|
||
// Parse the text | ||
var doc = parser.parseFromString(html, "text/html") | ||
|
||
// You can now even select part of that html as you would in the regular DOM | ||
// Example: | ||
// var docArticle = doc.querySelector('article').innerHTML; | ||
|
||
let formId = doc.querySelector("input[name='fid']").getAttribute("value") | ||
console.log(formId) | ||
console.log(doc) | ||
const formPromise = axios.$get( | ||
`api/1.1/events/form/${formId}` | ||
) | ||
|
||
formPromise.then((response) => { | ||
console.log(response) | ||
return response | ||
/* const jsonPromise = response.json() | ||
jsonPromise.then((data) => { | ||
console.log(data) | ||
return data | ||
}) */ | ||
}) | ||
|
||
|
||
}) | ||
.catch(function(err) { | ||
console.log('Failed to fetch page: ', err) | ||
}) | ||
} | ||
|
||
export default scrapeFormId |
5829229
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://uclalibrary-test.library.ucla.edu as production
🚀 Deployed on https://635b04b3833154007f3fd087--uclalibrary-test.netlify.app