rkd77 / elinks

Fork of elinks
Other
349 stars 38 forks source link

Python error when calling elinks -dump from within Newsboat browser. #292

Closed ZaxonXP closed 5 months ago

ZaxonXP commented 7 months ago

I use Newsboat RSS reader to get new articles. The ones which I want to read I dump as a text file using a macro script which is calling Elinks browser. The script is as follows:

#!/bin/bash
IFS=$'\n'
data=( $(cat) )
URL=$(echo ${data[4]}| cut -d ":" -f 2,3)
TITLE=$(echo ${data[1]}| cut -d ":" -f 2)
TITLE=${TITLE/ /}
URL=${URL/ /}

# store article locally
elinks -no-connect 1 -no-numbering -dump "$URL" > ~/.newsboat/offline/"$TITLE".txt
echo "$URL" >> ~/.newsboat/offline/"$TITLE".txt

This is assigned in the ~/.newsboat/config as a macro

macro a pipe-to ~/.local/scr/ela

So when I use the macro I get the following error: 2024-03-19-093949_1920x1080_scrot

Here is a content of my hooks.py script:

import elinks
import fcntl
import termios
import os

def pre_format_html_hook(url, html):
    with open('/dev/stdin', 'w') as fd:
        for char in "Ma":
           fcntl.ioctl(fd, termios.TIOCSTI, char)

def try_to_reload():
    """Try to reload."""
    with open('/dev/stdin', 'w') as fd:
        for char in "maR":
           fcntl.ioctl(fd, termios.TIOCSTI, char)

def copy_url():
    """ Copy current url """
    os.system('clear')
    print(elinks.current_url())

elinks.bind_key("F7", copy_url)
elinks.bind_key("Ctrl-r", try_to_reload)

How can get rid of this error message?

Kind regards, Piotr

rkd77 commented 7 months ago

You can try wrap fcntl.ioctl with try: except: Example:

def pre_format_html_hook(url, html):
    with open('/dev/stdin', 'w') as fd:
        for char in "Ma":
           try:
               fcntl.ioctl(fd, termios.TIOCSTI, char)
           except Exception as e:
               pass

The second fcntl in similar way.

ZaxonXP commented 7 months ago

That worked well. Article was stored and no more error is displayed. Thanks!

ZaxonXP commented 5 months ago

@rkd77: I have some remaining small issue with this solution. Currently when I do dumping the page to the text file the Ma word is also send to the terminal. This screws up my another script where I use the fzf/dmenu for selection what to do with the saved article. After elinks dumps the file it also typing Ma on the terminal which select non existing item.

Is there a way to avoid printing this Ma word to the terminal?

I think the reason it was used is to jump back to the marked a position after reloading the page.

rkd77 commented 5 months ago

There is no good solution, because Python hooks have no access to browser options AFAIR. You can workaround it like this: Before dump, touch some file, check in python pre_format hook whether touched file exists and return if exists, dump. remove touched file.

touch /tmp/dump
elinks --dump someurl
rm -f /tmp/dump

At the begining of pre_format_html_hook:

if os.path.exists('/tmp/dump'):
    return
ZaxonXP commented 5 months ago

Your solution was good. It is working and creating temp file is no problem. Thanks!