Programming thread

  • Want to keep track of this thread?
    Accounts can bookmark posts, watch threads for updates, and jump back to where you stopped reading.
    Create account
Guys, I have an ask referring to something I posted over in the Archival Tools thread. Please don't throw rocks at me.

Bash:
#!/bin/bash
#
# archive.today link script
#

cwd=$(pwd)
shortname=$(echo "$1" | sed 's/\.txt$//')

if [[ -z "$1" ]]; then
  echo "Error: No filename provided"
  exit
fi

if [[ ! -f "$1" ]] ; then
  echo "Error: File does not exist"
  exit
fi

if [[ ! -s "$1" ]] ; then
  echo "Error: File is empty"
  exit
fi

urls=$(while read -r line; do
  echo -n '  <DT><A HREF="https://archive.today/?run=1&url='
  echo ''"$line"'">'"$line"'</A>'
done <"$1")
touch "$shortname".html
cat >"$shortname".html <<CREATEHTML
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<html>
<head>
<title>$shortname</title>
<style>
body {
    background-color: #000000;
    color: #ffffff;
}
</style>
<b>$shortname</b><br>
<br>
Archived urls:<br>
<dl><p>
$urls
</dl><p>
</head>
</html>
CREATEHTML

echo 'file://'"$cwd"'/'"$shortname"'.html'

brave-browser file://"$cwd"/"$shortname".html &>/dev/null &
#flatpak run com.brave.Browser file://"$cwd"/"$shortname".html 2>/dev/null &
a real quick improvement would be to replace the direct call to brave-browser with xdg-open which would instead open a users preferred browser and remove the dependency on brave
 
a real quick improvement would be to replace the direct call to brave-browser with xdg-open which would instead open a users preferred browser and remove the dependency on brave
Thanks, I originally was setting it to use Brave because Mullvad Browser would make me solve a new captcha with every new link I clicked around the time it first released. I just tested it again and that doesn't happen any more.
 
Guys, I have an ask referring to something I posted over in the Archival Tools thread. Please don't throw rocks at me.
I want to help you out but can you post a small .zip containing one folder for intended input and another for the corresponding output? (I really do not write shell scripts anymore.)

Also, xdg-settings set default-web-browser your-browser.desktop (e.g. firefox.desktop; you may have to do some digging with find or locate (and remember to keep the locate database updated))
 
Guys, I have an ask referring to something I posted over in the Archival Tools thread. Please don't throw rocks at me.
nice shitscript
my only real recommendation is that you could swap out brave-browser with xdg-open which will use the desktop environment to automatically open the html file with whatever is associated to it
or just leave that out
otherwise as long as it works, it fucking works, you know

if i did it i would make it be a sed or something that appends all the urls and prints them to the terminal and i just paste all those in the browser
a real quick improvement would be to replace the direct call to brave-browser with xdg-open which would instead open a users preferred browser and remove the dependency on brave
i'd probably just make it not automatically open the browser, i hate shit automatically opening my browser
I want to help you out but can you post a small .zip containing one folder for intended input and another for the corresponding output? (I really do not write shell scripts anymore.)
the intended input here is a text file with 1 url per line and the intended output is an html file containing links to archive.today's "archive now" link for each of those urls
 
I want to help you out but can you post a small .zip containing one folder for intended input and another for the corresponding output? (I really do not write shell scripts anymore.)
Code:
https://en.wikipedia.org/wiki/Action_selection
https://en.wikipedia.org/wiki/Activation_function
https://en.wikipedia.org/wiki/Adobe_Firefly
https://en.wikipedia.org/wiki/Adversarial_machine_learning
https://en.wikipedia.org/wiki/AI-assisted_software_development
https://en.wikipedia.org/wiki/Aidan_Gomez
https://en.wikipedia.org/wiki/AI_prompt
https://en.wikipedia.org/wiki/Alan_Turing
https://en.wikipedia.org/wiki/Aleph_Alpha
edit: this is the output, first time had extra white space because I'm a tard.
HTML:
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<html>
<head>
<title>list</title>
<style>
body {
    background-color: #000000;
    color: #ffffff;
}
</style>
<b>list</b><br>
<br>
Archived urls:<br>
<dl><p>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Action_selection">https://en.wikipedia.org/wiki/Action_selection</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Activation_function">https://en.wikipedia.org/wiki/Activation_function</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Adobe_Firefly">https://en.wikipedia.org/wiki/Adobe_Firefly</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Adversarial_machine_learning">https://en.wikipedia.org/wiki/Adversarial_machine_learning</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/AI-assisted_software_development">https://en.wikipedia.org/wiki/AI-assisted_software_development</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Aidan_Gomez">https://en.wikipedia.org/wiki/Aidan_Gomez</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/AI_prompt">https://en.wikipedia.org/wiki/AI_prompt</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Alan_Turing">https://en.wikipedia.org/wiki/Alan_Turing</A>
  <DT><A HREF="https://archive.today/?run=1&url=https://en.wikipedia.org/wiki/Aleph_Alpha">https://en.wikipedia.org/wiki/Aleph_Alpha</A>
</dl><p>
</head>
</html>
otherwise as long as it works, it fucking works, you know
That was my initial thought, thanks for the confirmation.
 
Last edited:
Guys, I have an ask referring to something I posted over in the Archival Tools thread. Please don't throw rocks at me.
I can do it in fsharp to run in fsi, I guess?

Should be executable by just running "dotnet fsi <scriptfile name>" Dunno about starting the browser as well. You are likely to have .net already installed on your system, so no additional dependencies.

PS DT tags aren't self contained, unlike br tags.

Code:
open System
open System.IO

let filename = Console.ReadLine()
if filename = "" then
  Console.WriteLine "Error: No filename provided"
  exit -1
if not <| File.Exists (filename + ".txt") then
  Console.WriteLine "Error: File does not exist"
  exit -1
 
let urls = File.ReadAllLines (filename + ".txt")
if urls.Length = 0 then
  Console.WriteLine "Error File is empty"
  exit -1
 
let links =
  Array.map (fun x -> $"""<dt><a href="https://archive.today/?run=1&url={x}">{x}</a></dt>""") urls
  |> Array.reduce (fun fst snd -> fst + snd)
let data = $$"""
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<html>
<head>
<title>{{filename}}</title>
<style>
body {
    background-color: #000000;
    color: #ffffff;
}
</style>
<b>{{filename}}</b><br>
<br>
Archived urls:<br>
<dl><p>
{{links}}
</dl><p>
</head>
</html>
"""

let path = Directory.GetCurrentDirectory() + "/" + filename + ".html"
File.WriteAllText (path, data)
Console.WriteLine ("file://"+path)
 
Last edited:
Hm, I actually didn't know that. Turns out the version of lua that happens to be in my profile is only there because it's a propagated input of mpv. I don't know if that's a peculiarity of guix's packaging or if many packages that use lua are sticking with 5.2.
Code:
;; XXX: lua > 5.2 is not currently supported; see meson.build
lua-5.2
- guix package sources

it's either mpv being weird or a guix moment or maybe a little of both
also i love the fact that package definitions are always like 4 keystrokes away on any guix-enabled system
 
@clipartfan92

Looks like I got beaten to the punch but here's my submission in Python (only uses the standard library):
Python:
#!/usr/bin/env python

import os
import sys

if len(sys.argv) == 1:
    raise FileNotFoundError("No filename provided")

input_file = sys.argv[1]
basename = os.path.splitext(input_file)[0]

if not os.path.exists(sys.argv[1]):
    raise FileNotFoundError("File does not exist")
elif os.stat(sys.argv[1]).st_size == 0:
    raise EOFError("File is empty")

urls = []

with open(input_file) as f:
    for line in f:
        urls.append(line.strip())

url_text = ""

for url in urls:
    url_text += (
        f'  <dt><a href="https://archive.today/?run=1&url={url}">{url}</a></dt>\n'
    )

# Extra newline not needed
url_text = url_text.rstrip()

with open(f"{basename}.html", "w") as file:
    page = f"""<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<html>
<head>
<title>{basename}</title>
<style>
body {{
    background-color: #000000;
    color: #ffffff;
}}
</style>
<b>{basename}</b><br>
<br>
Archived urls:<br>
<dl><p>
{url_text}
</dl><p>
</head>
</html>
"""

    file.write(page)

print(f"file://{os.getcwd()}/{basename}.html")
I tried to match the original bash as closely as possible so that you can get a good idea of what it is I'm doing. One thing you should know is that I made substantial use of Python f-strings. I would also say what I did was very quick and dirty. It's hardly ideal. But it does work. I would also say that I am of course biased but I consider the Python script to be far more readable than the shell equivalent. It's not you, really; it's that shell tends to suffer from low readability in general.
I can do it in fsharp to run in fsi, I guess?

Should be executable by just running "dotnet fsi <scriptfile name>" Dunno about starting the browser as well. You are likely to have .net already installed on your system, so no additional dependencies.

PS DT tags aren't self contained, unlike br tags.

Code:
open System
open System.IO

let filename = Console.ReadLine()
if filename = "" then
  Console.WriteLine "Error: No filename provided"
  exit -1
if not <| File.Exists (filename + ".txt") then
  Console.WriteLine "Error: File does not exist"
  exit -1
 
let urls = File.ReadAllLines (filename + ".txt")
if urls.Length = 0 then
  Console.WriteLine "Error File is empty"
  exit -1
 
let links =
  Array.map (fun x -> $"""<dt><a href="https://archive.today/?run=1&url="{x}">x</a></dt>""") urls
  |> Array.reduce (fun fst snd -> fst + snd)
let data = $$"""
<!DOCTYPE html>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<html>
<head>
<title>$shortname</title>
<style>
body {
    background-color: #000000;
    color: #ffffff;
}
</style>
<b>{{filename}}</b><br>
<br>
Archived urls:<br>
<dl><p>
{{urls}}
</dl><p>
</head>
</html>
"""

let path = Directory.GetCurrentDirectory() + filename + ".html"
File.WriteAllText (path, data)
Console.WriteLine ("file://"+path)
I don't really know F# well but it looks like you wanted to put links in the HTML document?
 
Last edited:
In fact I did, thanks for spotting that. That's what I get for shitting out a script without testing. Actually ran and tested it now, found a few other errors I also fixed.
I'm a big fan of compulsively checking and rechecking things (like doing git statusfrequently, sometimes even after things look OK in Magit or Lazygit). You will spot way more errors that way.
 
I'm a big fan of compulsively checking and rechecking things (like doing git statusfrequently, sometimes even after things look OK in Magit or Lazygit). You will spot way more errors that way.
Yeah, I should've actually ran the script at all. That's what I get for my hubris.

Also, gotta love the extended interpolation syntax that f# provides, I see you used a similar feature with your python script. Nvm, seems like you "just" escaped curly braces by doubling up on them

For git, I pretty much exclusively use the CLI, because I've found it to be the most convenient tool for me. It has certainly gotten me out of some fuck ups that resulted in my local git directory resembling the local haunted house.

Oh and looks like you went with passing in the filename as a startup parameter rather than getting it via user input. Yeah, that seems like the proper way, but too busy to fix that up atm.
 
For git, I pretty much exclusively use the CLI, because I've found it to be the most convenient tool for me.
I would recommend using a Git interface for staging and committing individual hunks rather than staging them all at once (or fighting the CLI tools). I don't use too many advanced features but I wanted to squash a few commits like yesterday or the day before and found it was less painful to do all of that in Lazygit as opposed to git rebase -i or whatever from the command line.
 
I would recommend using a Git interface for staging and committing individual hunks rather than staging them all at once (or fighting the CLI tools). I don't use too many advanced features but I wanted to squash a few commits like yesterday or the day before and found it was less painful to do all of that in Lazygit as opposed to git rebase -i or whatever from the command line.
Staging files is in fact the one thing where I use a gui tool (in my case, the rider inbuilt git tool) rather than the cli.
 
C-like:
(import (scheme base)
        (scheme file)
        (scheme process-context)
        (scheme cxr)
        (srfi srfi-13))

(define (remove-ext name)
  (string-take name (string-rindex name #\.)))

(if (< (length (command-line)) 2)
    (error "No input file name"))

(define input-file-name (cadr (command-line)))
(define base-file-name (remove-ext input-file-name))
(define output-file-name (string-append base-file-name ".html"))

(if (file-exists? output-file-name)
    (error "Output file already exists"))

;; and now we actually write the file
(with-output-to-file output-file-name
  (λ ()
    (with-input-from-file input-file-name
      (λ ()
        ;; prologue
        (display "<html><head><meta charset=\"utf-8\"><title>")
        (display base-file-name)
        (display "</title><style>body{background-color: black; color: white;}</style></head>")
        (newline)
        (display "<body><h1>")
        (display base-file-name)
        (display "</h1><h2>Archive links:</h2>")

        ;; list of links
        (display "<ul>")
        (newline)
        (let lp ()
          (let ((line (read-line)))
            (when (not (eof-object? line))
              (begin
                (display "<li><a href=\"https://archive.today/?run=1&url=")
                (display line)
                (display "\">")
                (display line)
                (display "</a></li>")
                (newline)
                (lp)))))
        (display "</ul>")
        (newline)

        ;; epilogue
        (display "</body></html>")
        (newline)))))
here's a version in mostly pure (i imported srfi-13) r7rs-small scheme while we're at it
i made a couple of small changes to the behavior such as checking if the output file already exists (i don't really like clobbering files...)
i also made the html simpler and removed some of the error handling that i don't agree with (an empty file is not the end of the world)
it's slightly suboptimal but not bad

i also forgot to print the filename at the end but idc
 
Last edited:
You don't even want to know how much adderall was involved with getting this to work with zero idea of what I was doing.
I know you don't like it, but this is reasonably well-written bash script, insofar as such things can be considered reasonably well-written.

Your "touch" command is extraneous. Your cwd, shortname, and urls assignments will break on URLs with spaces in them but they ought to be URL-escaped anyhow, which is fine. In double [[]] you don't need quotes around your variables. You do error checking.

It's only ugly because you're working in bash. Your development practices look well-thought-out.
 
It's only ugly because you're working in bash. Your development practices look well-thought-out.
it's not particularly ugly. a retard (me) can figure out what it's supposed to do and how it does it and why it does the things that it does
the syntax is fucking shit but that's an inherent problem of the language and not anything that op did

personally i think my scheme version looks slightly better than the other versions. no i am not biased stalker and you will be enjoying parenthesis prison
 
Back
Top Bottom