help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use variable from BEGIN as input to remaining program


From: Z
Subject: Re: Use variable from BEGIN as input to remaining program
Date: Wed, 06 Sep 2023 21:34:29 +0000
User-agent: mail v14.9.24

port19 <port19@port19.xyz> wrote:

> Hi all,
> I have now managed to send https requests via the use of stunnel.
> My current problem is that it seems impossible to reuse the response.
> In my limited research I did not manage to find ways to define records in 
> BEGIN.
> 
> Is it possible to use output retrieved in BEGIN as input for the rest of the 
> program?
> If yes, how so? If not, how would you proceed?
> 
> Context is the rewrite of a small reddit client. [1]
> 
> ~port19
> 
> [1] https://github.com/port19x/redqu/tree/gawk

Hello,
This seems to sort of do what you want; I used your stunnel.conf settings and
simplified redqu.awk some for testing purposes (just extracts i.redd.t/*.jpg):

--
#!/usr/bin/gawk -E
BEGIN {
  subreddit = "cats"
  sort = "/top.rss"
  time = "?t=hour"
  endpoint = "/r/" subreddit sort time

  print endpoint

  target = "/inet/tcp/0/localhost/111"
  agent = "Mozilla/5.0 (X11; Linux x86_64; rv:101.0) Gecko/20100101 
Firefox/101.0"
  payload = "GET " endpoint " HTTP/1.1" "\r\n" "Host: www.reddit.com:443" 
"\r\n" "User-Agent: gawk" "\r\n" "Connection:close" "\r\n\r\n"

  print payload |& target

  while ((target |& getline line) > 0)
    response = response " " line
  close(target)

 split(response,array,"[&]quot;")

  for (i in array)
    if (array[i] ~ /i[.]redd[.]it[/][a-z0-9]*[.](png|jpg|gif)?/)
      print array[i]
}
--

# test run:
$ redqu.awk
/r/cats/top.rss?t=hour
https://i.redd.it/grp3q71s6pmb1.jpg
https://i.redd.it/iwk9813y8pmb1.jpg
https://i.redd.it/860mvxdo5pmb1.jpg
https://i.redd.it/dgl94msacpmb1.jpg
https://i.redd.it/nau8nfiy4pmb1.png
https://i.redd.it/s876wpov8pmb1.jpg
https://i.redd.it/06zs9ypg5pmb1.jpg
https://i.redd.it/inbr09g19pmb1.jpg
https://i.redd.it/mqq0a0j27pmb1.jpg
https://i.redd.it/gowtqc5m4pmb1.png
https://i.redd.it/1kx58or5cpmb1.png
https://i.redd.it/ceq7jx398pmb1.jpg
https://i.redd.it/gdapriog5pmb1.jpg
https://i.redd.it/aq5z64tncpmb1.jpg
https://i.redd.it/p36owsm1cpmb1.jpg
https://i.redd.it/kipkbbywbpmb1.jpg




reply via email to

[Prev in Thread] Current Thread [Next in Thread]