Last active
June 20, 2024 14:35
-
-
Save umberto10/b30e8ce4de90ccbe23fbe673c3fe1146 to your computer and use it in GitHub Desktop.
pwr sks-cli in py and perl
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use LWP::UserAgent; | |
my $URL = 'https://sks.pwr.edu.pl/menu/'; | |
my $oHTTPAgent = new LWP::UserAgent; | |
my $oRequest = HTTP::Request->new('GET'); | |
$oRequest->url($URL); | |
my $sResponse = $oHTTPAgent->request($oRequest); | |
if ($sResponse->is_success) { | |
my $sPage = $sResponse->content; | |
my @page = split('\n', $sPage); | |
my $cnt = 0; | |
foreach my $line (@page) { | |
my @header = $line =~ /<h2>(.+)<\/h2>/mg; | |
if (@header) { | |
$cnt++; | |
printf("\n\t=== %s ===\n", @header); | |
next; | |
} | |
my @ritem = $line =~ /\s+(.+)<span.*$/mg; | |
my $item = join('', @ritem); | |
$item =~ s/\s+$//; | |
chomp($item); | |
my @rprice = $line =~ /.*<span class="price"> (.+)<.*/mg; | |
my $price = join('', @rprice); | |
$item=~ s/\s+$//; | |
chomp($price); | |
if (@ritem && @rprice) { | |
my $tmp = $item; | |
my $pl = $tmp =~ tr/[A-Za-z0-9. \/]//cd; | |
my $pad = 45 + ($pl / 2); | |
printf("\t%-${pad}s %6.2f zł\n", $item, $price); | |
} | |
} | |
if ($cnt == 0) { | |
print("\nNieczynne :(\n"); | |
} | |
print("\n") | |
} |
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
#!/usr/bin/env python3 | |
from bs4 import BeautifulSoup | |
import requests | |
r = requests.get('https://sks.pwr.edu.pl/menu/') | |
r.encoding = "utf-8" | |
page = BeautifulSoup(r.text, 'html.parser') | |
menu = page.find(id="menu_table") | |
categories = menu.find_all(class_="category") | |
if (len(categories) == 0): | |
print("\nNieczynne :(\n") | |
exit(0) | |
print() | |
for c in categories: | |
print("\t=== {} ===".format(c.find(class_='cat_name').h2.text)) | |
items = c.find('ul') | |
for li in items.find_all("li"): | |
item = li.contents[0].strip() | |
price = li.contents[1].string.strip() | |
print("\t{:<45} {:>6} zł".format(item, price)) | |
print() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment