XenoTheStrange

Sherms: Are you hiring yet? Checker

Dec 31st, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | None | 0 0
  1. #!/bin/lua
  2.  
  3. -- The purpose of this script is to tell me if the local Sherms locations are hiring at all and for what positions. Made bit by bit with the help of chatGPT, i.e. haphazardly slapped together.
  4.  
  5. --Output looks like this (potentially):
  6. --Job Title: Checker - Part Time
  7. --Location:
  8. --                Klamath Falls Thunderbird
  9. --                <br>
  10. --
  11. --
  12. --Job Title: Courtesy Clerk - Part Time
  13. --Location:
  14. --                Klamath Falls Thunderbird
  15. --                <br>
  16. --
  17. --
  18.  
  19. local http = require("socket.http") -- also luasocket(?)
  20. local ltn12 = require("ltn12") -- luasocket
  21. local lpeg = require("lpeg") -- lpeg
  22.  
  23. -- Function to fetch HTML from a URL
  24. function fetch_html(url)
  25.     local response_body = {}
  26.     local res, code, response_headers, status = http.request{
  27.         url = url,
  28.         sink = ltn12.sink.table(response_body),
  29.         headers = {
  30.             ["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0"
  31.         }
  32.     }
  33.  
  34.     if not res then
  35.         print("Failed to fetch the webpage. HTTP code:", code)
  36.         return nil
  37.     end
  38.  
  39.     return table.concat(response_body)
  40. end
  41.  
  42. -- Function to remove HTML tags
  43. function removeHtmlTags(str)
  44.     return str:gsub("<[^>]+>", "")  -- Remove everything between < and >
  45. end
  46.  
  47. -- Function to extract job listings
  48. function extractJobListings(html, locationToCheck)
  49.     -- Pattern to capture job title and location
  50.     local jobPattern = "<a href[^\>]+>(.-)</a>.-<td>(.-)</td>"
  51.  
  52.     local foundJobs = {}
  53.  
  54.     -- Find all job entries
  55.     for title, location in string.gmatch(html, jobPattern) do
  56.         -- Remove HTML tags from title
  57.         title = removeHtmlTags(title)
  58.  
  59.         -- If the location matches the specified location
  60.         if location:match(locationToCheck) then
  61.             table.insert(foundJobs, {title = title, location = location})
  62.         end
  63.     end
  64.  
  65.     -- Display the results
  66.     if #foundJobs == 0 then
  67.         print("No current openings for " .. locationToCheck)
  68.     else
  69.         for _, job in ipairs(foundJobs) do
  70.             print("Job Title: " .. job.title)
  71.             print("Location: " .. job.location)
  72.             print("---")
  73.         end
  74.     end
  75. end
  76.  
  77.  
  78.  
  79. -- Main function to fetch and check for openings
  80. function check_openings(url, location)
  81.     local html_content = fetch_html(url)
  82.     if html_content then
  83.         extractJobListings(html_content, location)
  84.         else
  85.         print("Failed to fetch or process the HTML content.")
  86.     end
  87. end
  88.  
  89. -- Test the function for "Klamath Falls Thunderbird"
  90. local url = "https://www.shermsmarkets.com/Pages/13803/"
  91. local location = "Klamath"
  92. check_openings(url, location)
  93.  
Advertisement
Add Comment
Please, Sign In to add comment