1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

Port before_v0.60/progress_bar before_v0.60/weather before_v0.60/webscrapping (#846)

This PR is part of porting all old scripts #221 and includes only
removing already ported modules:

- `progress_bar` -> `sourced/progress_bar`
- `weather` ->  `modules/weather`
- `webscrapping` -> `sourced/webscrapping`
This commit is contained in:
Igor 2024-05-25 22:06:00 +04:00 committed by GitHub
parent 15cb7179e2
commit 93e71fe6a8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 0 additions and 409 deletions

View file

@ -1,22 +0,0 @@
def loading [] {
$"Loading (char newline)" | autoview
echo 0..100 | each { |tick|
sleep 50ms
# I believe '1000D' means move the cursor to the left 1000 columns
$"(ansi -e '1000D')" | autoview
$"($tick)%" | autoview
}
#show_cursor
}
def show_cursor [] {
$"(ansi -e '?25h')" | autoview
}
def hide_cursor [] {
$"(ansi -e '?25l')" | autoview
}
hide_cursor
loading
show_cursor

View file

@ -1,54 +0,0 @@
# progress bar attempt
# https://askubuntu.com/questions/747143/create-a-progress-bar-in-bash
# https://www.shellscript.sh/tips/progressbar/
# There is a strange artifact drawing the first two full blocks
# You can see this artifact better in progress_bar_no_back.nu
# I'm not sure what's going on nor how to fix it.
let pb_len = 25
let bg_fill = "▒" # Fill up to $pb_len
let blocks = ["▏" "▎" "▍" "▌" "▋" "▊" "▉" "█"]
# "█" #8/8
# "▉" #7/8
# "▊" #3/4
# "▋" #5/8
# "▌" #1/2
# "▍" #3/8
# "▎" #1/4
# "▏" #1/8
# Turn off the cursor
ansi cursor_off
# Move cursor all the way to the left
$"(ansi -e '1000D')" | autoview
# Draw the background for the progress bar
$bg_fill | str lpad -c $bg_fill -l $pb_len
echo 1..<$pb_len | each { |cur_progress|
# This is kind of a hack because it's not incrementally drawing a new box
# It's drawing the entire row every time with a different padding amount
# echo $blocks.7 | str lpad -c $blocks.7 -l $it | autoview
echo 0..7 | each { |tick|
let cur_idx = ($tick mod 8)
let cur_block = (echo $blocks | nth $cur_idx)
$"($cur_block | str lpad -c $blocks.7 -l $cur_progress)" | autoview
$"(ansi -e '1000D')" | autoview
sleep 5ms
}
$"(ansi -e '1000D')" | autoview
}
# Fill in the last background block
$"($blocks.7 | str lpad -c $blocks.7 -l $pb_len)" | autoview
char newline
"Done"
ansi cursor_on
# Try to do this in the next version
# Make it a custom command so you can do
# set-progress 33 100
# and the display look like
# 33% (33/100) [███████████ ]

View file

@ -1,16 +0,0 @@
let blocks = ["▏" "▎" "▍" "▌" "▋" "▊" "▉" "█"]
let pb_size = 25
ansi cursor_off
echo 1..<$pb_size | each { |cur_size|
echo 0..7 | each { |tick|
let idx = ($tick mod 8)
let cur_block = (echo $blocks | nth $idx)
$"($cur_block | str lpad -c $blocks.7 -l $cur_size)" | autoview
$"(ansi -e '1000D')" | autoview
sleep 5ms
}
}
char newline
'Done'
ansi cursor_on

View file

@ -1,5 +0,0 @@
# Weather Scripts
### Definition
These scripts should be used to demonstrate how get your local weather and/or weather forecasts.

View file

@ -1,232 +0,0 @@
###################################################
## Weather Script based on IP Address v1.0
###################################################
def locations [] {
[
[location city_column state_column country_column lat_column lon_column];
["http://ip-api.com/json/" city region countryCode lat lon]
["https://ipapi.co/json/" city region_code country_code latitude longitude]
["https://freegeoip.app/json/" city region_code country_code latitude longitude]
["https://ipwhois.app/json/" city region country_code latitude longitude]
]
}
def get_my_location [index: int] {
let loc_json = (fetch (locations | nth $index).location)
let city_column = (locations | nth $index).city_column
let state_column = (locations | nth $index).state_column
let country_column = (locations | nth $index).country_column
let lat_column = (locations | nth $index).lat_column
let lon_column = (locations | nth $index).lon_column
# echo $loc_json
if ($city_column | str length) > 1 {
if ($state_column | str length) > 1 {
if ($country_column | str length) > 1 {
let lookup_state = ($loc_json | get ($state_column | into column-path))
if ($lookup_state | str length) > 2 {
let state = (state_abbrev_lookup $lookup_state)
$"($loc_json | get ($city_column | into column-path)),($state),($loc_json | get ($country_column | into column-path))"
} {
$"($loc_json | get ($city_column | into column-path)),($loc_json | get ($state_column | into column-path)),($loc_json | get ($country_column | into column-path))"
}
} {
$"($loc_json | get ($city_column | into column-path)),($loc_json | get ($state_column | into column-path))"
}
} {
$"($loc_json | get ($city_column | into column-path))"
}
} {
"No City Found"
}
}
let URL_QUERY_LOCATION = "https://api.openweathermap.org/geo/1.0/direct"
let TOKEN = "85a4e3c55b73909f42c6a23ec35b7147"
def get_location_by_ip [locIdx: int] {
let location = (get_my_location $locIdx)
let url = $"($URL_QUERY_LOCATION)?q=($location)&limit=5&appid=($TOKEN)"
# echo $url
fetch $url
}
let URL_WEATHER = "https://api.openweathermap.org/data/2.5/weather"
def get_weather_by_ip [locIdx: int, units: string] {
# units
# f = imperial aka Fahrenheit
# c = metric aka Celsius
let coords = (get_location_by_ip $locIdx)
if ($coords | length) > 1 {
[
[msg, labels, span];
["Error getting location", "There were more than one locations found", ([[start, end]; [0, 1]])]
] | error make
} { }
if $units == "f" {
let units = "imperial"
let url = $"($URL_WEATHER)?lat=($coords.lat)&lon=($coords.lon)&units=($units)&appid=($TOKEN)"
# echo $url (char nl)
let weather = (fetch $url)
# echo $weather | pivot | flatten | flatten
[
['Weather Location' Longitude Latitude Temperature 'Feels Like' Humidity Pressure Emoji];
[$"($weather.name), ($weather.sys.country)" ($weather.coord.lon) ($weather.coord.lat) $"($weather.main.temp | into string -d 1) °F" $"($weather.main.feels_like | into string -d 1) °F" ($weather.main.humidity) ($weather.main.pressure) (get_icon_from_table $weather.weather.main.0)]
]
} {
let units = "metric"
let url = $"($URL_WEATHER)?lat=($coords.lat)&lon=($coords.lon)&units=($units)&appid=($TOKEN)"
# echo $url (char nl)
let weather = (fetch $url)
# echo $weather | pivot | flatten | flatten
[
['Weather Location' Longitude Latitude Temperature 'Feels Like' Humidity Pressure Emoji];
[$"($weather.name), ($weather.sys.country)" ($weather.coord.lon) ($weather.coord.lat) $"($weather.main.temp | into string -d 1) °C" $"($weather.main.feels_like | into string -d 1) °C" ($weather.main.humidity) ($weather.main.pressure) (get_icon_from_table $weather.weather.main.0)]
]
}
}
def weather_emoji_table [] {
[
[name, icon];
['Clear', (char sun)]
['Clouds', (char clouds)]
['Rain', (char rain)]
['Fog', (char fog)]
['Mist', (char mist)]
['Haze', (char haze)]
['Snow', (char snow)]
['Thunderstorm', (char thunderstorm)]
]
}
def get_icon_from_table [w] {
weather_emoji_table | where name == $w | get icon
}
# Get the local weather by ip address
def get_weather [
--locIdx(-l): int # The location id 0-3
--units(-u): string # The units "f" or "c"
] {
let is_loc_empty = ($locIdx | empty?)
let is_units_empty = ($units | empty?)
let no_loc_no_unit = ($is_loc_empty == $true and $is_units_empty == $true)
let no_loc_with_unit = ($is_loc_empty == $true and $is_units_empty == $false)
let with_loc_no_unit = ($is_loc_empty == $false and $is_units_empty == $true)
let with_loc_with_unit = ($is_loc_empty == $false and $is_units_empty == $false)
# This is a cautionary tale, the commented out code below is returning
# and autoview is viewing the data, so no structured data is being returned.
# The ramification to this is you can't do get_weather | select Temperature Emoji
# like you should be able to. The following uncommented section below fixes it.
# Hopefully we'll be able to fix this somehow because it's easy to fall into
# this hole without knowing.
# if $no_loc_no_unit {
# echo "no_loc_no_unit"
# (get_weather_by_ip 0 "f")
# } { }
# if $no_loc_with_unit {
# echo "no_loc_with_unit"
# (get_weather_by_ip 0 $units)
# } { }
# if $with_loc_no_unit {
# echo "with_loc_no_unit"
# (get_weather_by_ip $locIdx "f")
# } { }
# if $with_loc_with_unit {
# echo "with_loc_with_unit"
# (get_weather_by_ip $locIdx $units)
# } { }
if $no_loc_no_unit {
(get_weather_by_ip 0 "f")
} { if $no_loc_with_unit {
(get_weather_by_ip 0 $units)
} { if $with_loc_no_unit {
(get_weather_by_ip $locIdx "f")
} { if $with_loc_with_unit {
(get_weather_by_ip $locIdx $units)
} { } }}}
}
def state_abbrev_lookup [state_name: string] {
# Weather Location 3 does not return state name abbreviations
# so we have to convert a state full name to a state abbreviation
let lookup_table = (
[
[state abbrev];
["Alabama" "AL"]
["Alaska" "AK"]
["Arizona" "AZ"]
["Arkansas" "AR"]
["California" "CA"]
["Colorado" "CO"]
["Connecticut" "CT"]
["Delaware" "DE"]
["Florida" "FL"]
["Georgia" "GA"]
["Hawaii" "HI"]
["Idaho" "ID"]
["Illinois" "IL"]
["Indiana" "IN"]
["Iowa" "IA"]
["Kansas" "KS"]
["Kentucky" "KY"]
["Louisiana" "LA"]
["Maine" "ME"]
["Maryland" "MD"]
["Massachusetts" "MA"]
["Michigan" "MI"]
["Minnesota" "MN"]
["Mississippi" "MS"]
["Missouri" "MO"]
["Montana" "MT"]
["Nebraska" "NE"]
["Nevada" "NV"]
["New Hampshire" "NH"]
["New Jersey" "NJ"]
["New Mexico" "NM"]
["New York" "NY"]
["North Carolina" "NC"]
["North Dakota" "ND"]
["Ohio" "OH"]
["Oklahoma" "OK"]
["Oregon" "OR"]
["Pennsylvania" "PA"]
["Rhode Island" "RI"]
["South Carolina" "SC"]
["South Dakota" "SD"]
["Tennessee" "TN"]
["Texas" "TX"]
["Utah" "UT"]
["Vermont" "VT"]
["Virginia" "VA"]
["Washington" "WA"]
["West Virginia" "WV"]
["Wisconsin" "WI"]
["Wyoming" "WY"]
]
)
$lookup_table | where state == $state_name | get abbrev
}
# To run this call
# > get_weather
# it will default to location 0 and Fahrenheit degrees
# > get_weather -l 1
# This changes to location 1. Locations are listed in the locations custom command above
# > get_weather -l 2 -u c
# This uses location 2 and Celsius degrees. f = Fahrenheit, c = Celsius
# Since I live in the USA I have not tested outside the country.
# We'll take PRs for things that are broke or augmentations.

View file

@ -1,65 +0,0 @@
# This script will run a command, in this case get_weather, at a prescribed interval
# This is meant to be run from your prompt so that it runs every time you cd or
# run a command. Each time it will check if it's been longer than the interval and
# if so, run the command, otherwise it uses the cached weather information.
# I wrote this so I could have weather in my prompt but not pay the price of hitting
# the web for every prompt. It will need to be tweaked to actually be used in a
# prompt, but for now it just prints the weather in these 3 ways.
# 1. if it's never been run, it runs the weather command and saves the json cache info
# 2. if the interval has not expired yet, it prints the Cached information
# 3. if the interval has expired, it runs the weather command again and caches the info
#command to run at interval
def timed_weather_run [
--command(-c): string # The command to run
--interval(-i): duration # The interval duration
] {
# get the type of system we're on
let system_name = ((sys).host | get name)
if $system_name == "Windows" {
# $"The system is Windows(char nl)"
# generate temp file name
let weather_runtime_file = (($nu.env.TMP) | path join weather_runtime_file.json)
# does the temp file already exist, meaning we've written it previously
if ($weather_runtime_file | path exists) == $true {
# $"Weather path exists [($weather_runtime_file)](char nl)"
# open the file and get the last weather data and run time out of it
let last_runtime_data = (open $weather_runtime_file)
# get the last runtime and add my timezone difference
let last_runtime = ($last_runtime_data | get last_run_time | str to-datetime)
if $last_runtime + $interval > (date now) {
# $"interval not met. last_runtime: [($last_runtime)](char nl)"
let temp = ($last_runtime_data | get Temperature)
let emoji = ($last_runtime_data | get Emoji)
$"Cached Temp: ($temp) Cached Emoji: ($emoji)"
} {
# save the run time and run the command
# $"interval met, running command: [($command)](char nl)"
# it would be nice to run a dynamic command here but i'm not sure it's possible yet
# let weather_table = (do { $command })
let weather_table = (if $command == "get_weather" {(get_weather)} {})
let temp = ($weather_table | get Temperature)
let emoji = ($weather_table | get Emoji)
$"Temp: ($temp) Emoji: ($emoji)"
$weather_table | insert last_run_time {(date now | date format -t '%Y-%m-%d %H:%M:%S %z')} | save $weather_runtime_file
}
} {
# $"Unable to find [($weather_runtime_file)], creating it(char nl)"
let weather_table = (get_weather)
let temp = ($weather_table | get Temperature)
let emoji = ($weather_table | get Emoji)
$"Created Temp: ($temp) Created Emoji: ($emoji)"
$weather_table | insert last_run_time {(date now | date format -t '%Y-%m-%d %H:%M:%S %z')} | save $weather_runtime_file
}
} {
# ToDo: refactor the info in the Windows section into another def. The only real difference
# is where the temp file will be located. Mac & Linux probably should be in /tmp I guess.
# everything else is linux or mac
}
}
source get-weather.nu
timed_weather_run --command "get_weather" --interval 1min

View file

@ -1,5 +0,0 @@
# Web scraping
### Definition
This folder includes examples of using nushell to scrape server side rendered websites

View file

@ -1,10 +0,0 @@
#!/usr/bin/env nu
let baseurl = 'https://www.schiit.co.uk/'
let pages = [headphone-amps dacs schiit-gaming-products power-amplifiers preamps upgrades accessories-cables schiit%20graded%20stock]
$pages|each {
fetch (build-string $baseurl $it)|selector -q 'div.caption' -m|each {
$it|selector -q 'p.stock, h5'|str trim|
rotate counter-clockwise t name availability|
reject t
}
}|sort-by availability