You are not logged in.

#3551 2021-05-08 12:39:40

dmz
Member
From: Sweden
Registered: 2008-08-27
Posts: 881
Website

Re: Post your handy self made command line utilities

MangoMan1 wrote:

First post here on the forums.

I love center. I'll be using it. Thanks!

Offline

#3552 2021-05-08 14:19:52

teckk
Member
Registered: 2013-02-21
Posts: 419

Re: Post your handy self made command line utilities

Get the arch news, several different ways, parse it several different ways.
Why? Because it's arch, you need the news, and it's also python practice. Few examples:

#!/usr/bin/python

#Get the Arch Latest News with xml.etree

from xml.etree import ElementTree
from urllib import request
import re

#Make a user agent string for urllib to use
agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) '
        'Gecko/20100101 Firefox/86.0')
        
user_agent = {'User-Agent': agent}

class MakeList():
    def __init__(self, url, fname):
    
        #Get the xml to parse
        req = request.Request(url, data=None, headers=user_agent)
        html = request.urlopen(req)
        tree = ElementTree.parse(html)
        root = tree.getroot()
        
        #Get tag data
        tagA = root.findall('./channel/item/title')
        tagB = root.findall('./channel/item/link')
        tagC = root.findall('./channel/item/description')
        tagD = []
        tagR = '<[^>]*>'
        
        #Append lines with separator
        for a,b,c in zip(tagA,tagB,tagC):
            tagD.extend([re.sub(tagR,'',a.text), re.sub(tagR,'',b.text), 
                        re.sub(tagR,'',c.text), '_' * 70])
                        
        
        #Print and Write list to file
        with open(fname, 'a') as f:
            for line in tagD:
                print(line, '\n')
                f.write(line + '\n'*2)
                
if __name__ == "__main__":
    
    #Urls, log names
    A = ('https://archlinux.org/feeds/news/', 'Arch_RSS.log')
    B = ('file:///local/file/archfeed.xml', 'test.log')
    
    #Choose RSS feed here
    url, fname = A

    MakeList(url, fname)
    
______________________________________________________________________

#!/usr/bin/python

#Get the Arch Latest News with xml.dom

from xml.dom import minidom
from urllib import request
import re

#Make a user agent string for urllib to use
agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) '
        'Gecko/20100101 Firefox/86.0')
        
user_agent = {'User-Agent': agent}

class MakeList():
    def __init__(self, url, fname):
        
        #Get the xml to parse
        req = request.Request(url, data=None, headers=user_agent)
        xml = request.urlopen(req)
        dat = minidom.parse(xml)
        
        #Get  tag data
        tagA = dat.getElementsByTagName('title')
        tagB = dat.getElementsByTagName('link')
        tagC = dat.getElementsByTagName('description') 
        tagD = [] 
        tagR = '<[^>]*>'
        
        #Append lines with separator
        for i in range(0, len(tagA)):
            A = (tagA[i].firstChild.data)
            B = (tagB[i].firstChild.data)
            C = (tagC[i].firstChild.data)
            tagD.extend([re.sub(tagR,'',A), re.sub(tagR,'',B), 
                        re.sub(tagR,'',C), '_' * 70])
                        
        #Print and Write list to file
        with open(fname, 'a') as f:
            for line in tagD:
                print(line, '\n')
                f.write(line + '\n'*2)
            
if __name__ == "__main__":
    
    #Urls, log names
    A = ('https://archlinux.org/feeds/news/', 'Arch_RSS.log')
    B = ('file:///local/file/archfeed.xml', 'test.log')
    
    #Choose RSS feed here
    url, fname = A

    MakeList(url, fname)
    
______________________________________________________________________
        
#!/usr/bin/python

#Get the Arch Latest News with Beautifulsoup

from bs4 import BeautifulSoup
from urllib import request
import re

agent = ('Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) '
        'Gecko/20100101 Firefox/86.0')
        
user_agent = {'User-Agent': agent}

class MakeList():
    def __init__(self, url, fname):

        #Get the xml to parse
        req = request.Request(url, data=None, headers=user_agent)
        html = request.urlopen(req)
        contents = html.read()
        soup = BeautifulSoup(contents,'xml')
        
        #Get tag data
        titles = soup.find_all('title')
        links = soup.find_all('link')
        del links[1]
        descriptions = soup.find_all('description')
        news=[]
        tagR = '<[^>]*>'
               
        #Append lines with separator
        for a,b,c in zip(titles,links,descriptions):
            news.extend([re.sub(tagR,'',a.text), re.sub(tagR,'',b.text), 
                        re.sub(tagR,'',c.text), '_' * 70])
                        
        #Print and Write list to file
        with open(fname, 'a') as f:
            for line in news:
                print(line, '\n')
                f.write(line + '\n'*2)

if __name__ == "__main__":
    
    #Urls, log names
    A = ('https://archlinux.org/feeds/news/', 'Arch_RSS.log')
    B = ('file:///local/file/archfeed.xml', 'test.log')
    
    #Choose RSS feed here
    url, fname = A

    MakeList(url, fname)
    
______________________________________________________________________
    
#!/usr/bin/python

#Get the Arch Latest News with http.client and re

from http.client import HTTPSConnection
import re

def getSource(url):
    #Get page source
    a = HTTPSConnection(url)
    a.request('GET', '/feeds/news/')
    b = a.getresponse()
    data = b.read().decode('utf-8', errors='ignore')
    a.close()
    
    #Write file, read it
    with open ('arch_rss.xml', 'w+') as f:
        for line in data.split('\n'):
            f.write(line)
        f.seek(0)
        xml = f.read()
        
        #Parse tags with re
        A = re.findall("<title>(.*?)</title>", xml)
        B = re.findall("<link>(.*?)</link>", xml)
        C = re.findall("<description>(.*?)</description>", xml)
        
        #Alternate A,B,C together
        D =[x for y in zip(A, B, C) for x in y]
        
        #Print parsed data
        for line in D:
            print(line, '\n')

url = 'archlinux.org'
getSource(url)

Offline

#3553 2021-05-15 13:45:14

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,560
Website

Re: Post your handy self made command line utilities

Lil' something that was a Bash script

package Fetchfriends;
use Carp;
use strict;
use warnings;
use File::Copy;
use MP4::Info;

our $VERSION = '1.0.0';

chdir 'C:\\Users\\ugjka\\FrancisFriendsM4A' or croak 'Files not found!';

system 'pip-review -a --user' and croak 'PIP update failed';

system 'youtube-dl', '-f', '140', '-i',
  '--download-archive', 'list.txt',
  '--add-metadata',
  'https://www.youtube.com/playlist?list=UUHBTezPh7u20jydYYg3j83Q',
  'https://www.youtube.com/playlist?list=UUAYslZlNuBDD7FMvpjWLtoQ',
  'https://www.youtube.com/playlist?list=UU8PdsoinYT_izA5t54l2flg',
  'https://www.youtube.com/playlist?list=UUxXWjjtATq3OM545gMh9PUg'
  and croak 'Video download failed';

my @files = <*.m4a>;
foreach my $file (@files) {
    my $tag = get_mp4tag($file) or croak 'No TAG info';
    if ( !length( $tag->{ART} ) ) {
        croak 'Artist tag empty';
    }

    my $wav     = $file =~ s/[.]m4a/.wav/rmsx;
    my $artist  = $tag->{ART};
    my $title   = $tag->{NAM};
    my $comment = $tag->{CMT};
    my $date    = $tag->{DAY};

    system 'ffmpeg', '-i', $file, '-f', 'wav', $wav
      and croak('Transcode to wav failed');

    unlink $file or croak "deleting ${file} failed";

    system 'fdkaac', '-b', '24000', '-p', '29',
      '--title', $title, '--artist', $artist, '--comment', $comment, '--date',
      $date, $wav
      and system 'fdkaac', '-b', '24000', '-p', '5',
      '--title', $title, '--artist', $artist, '--comment', $comment, '--date',
      $date, $wav
      and croak('Fdkaac encode failed');

    unlink $wav or croak "Deleting ${wav} failed";
    move $file, $artist or croak 'Moving the m4a file failed';

}

print "Uploading to Google Drive\n";
system
'rclone --log-level INFO copy C:\\Users\\ugjka\\FrancisFriendsM4A francis:FrancisFriendsM4A';

1;

Fix yo shit: journalctl -b -p warning
Github

Offline

#3554 2021-05-17 14:58:10

Alad
Wiki Admin/IRC Op/TU
From: Bagelstan
Registered: 2014-05-04
Posts: 2,177
Website

Re: Post your handy self made command line utilities

Lil' something that was a Bash script

Why do you hardcode the URLs in your script? Perl has ARGV like anything else. You could also store those URLs in a seperate file that is read from the script.

Last edited by Alad (2021-05-17 15:03:38)


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Honest Alad's Package Emporium—Now with added bugs! (Grand reopening: December 1st 2018)

Offline

#3555 2021-05-17 15:02:21

lenhuppe
Member
From: New Hampshire
Registered: 2018-12-10
Posts: 86
Website

Re: Post your handy self made command line utilities

Alad wrote:

Why do you hardcode the URLs in your script? Perl has ARGV like anything else. You could also store those URLs in a seperate file that is read from the script.

Which post are you referring to?

Offline

#3556 2021-05-17 15:03:19

Alad
Wiki Admin/IRC Op/TU
From: Bagelstan
Registered: 2014-05-04
Posts: 2,177
Website

Re: Post your handy self made command line utilities

The one above it... #3553. I'll edit in a quote.


Mods are just community members who have the occasionally necessary option to move threads around and edit posts. -- Trilby
Honest Alad's Package Emporium—Now with added bugs! (Grand reopening: December 1st 2018)

Offline

#3557 2021-05-17 16:41:29

ugjka
Member
From: Latvia
Registered: 2014-04-01
Posts: 1,560
Website

Re: Post your handy self made command line utilities

Alad wrote:

Lil' something that was a Bash script

Why do you hardcode the URLs in your script? Perl has ARGV like anything else. You could also store those URLs in a seperate file that is read from the script.

I know about ARGV and stuff, those URLs will never change and there probably won't be removals or additions. I don't see the point for extra logic but thanks for heads up smile

EDIT: those URLs aren't sensitive, just public channels of Advaita Vedanta teachers

Last edited by ugjka (2021-05-17 16:50:27)


Fix yo shit: journalctl -b -p warning
Github

Offline

#3558 2021-05-17 22:31:19

icar
Member
Registered: 2020-07-31
Posts: 163

Re: Post your handy self made command line utilities

I adapted the fzf one liner for pacman in https://wiki.archlinux.org/title/Fzf#Ar … c_fzf_uses for Flatpak, using fzf Rust counterpart skim. I know it's not a welcomed technology here, but I guess I'd share it:

flatpak remote-ls flathub --system --cached --app --columns=application:f,description:f | sk --multi --preview 'flatpak remote-info flathub {1}' --preview-window=right:30% | awk '{print \$1}' | xargs -ro flatpak install --system --or-update flathub

flatpak list --system --app --columns=application:f,description:s | sk --multi --preview 'flatpak info {1}' | xargs -ro flatpak uninstall

Offline

#3559 2021-06-02 17:29:34

lmn
Member
Registered: 2021-05-09
Posts: 3

Re: Post your handy self made command line utilities

I keep offline documentation for stuff I use regularly and use this script for selecting and opening.

#!/bin/sh

dir="${1:-.}"
[ ! -d "$dir" ] && exit 1 

cd "$dir"

find . -type f \
	| sed 's|^./||' \
	| sort -n \
	| fzf -e --reverse --height 40% --info=inline --border \
	| xargs -r xdg-open 

fzf can be replaced by some other selector.
sort and sed are just for cosmetics.

Offline

#3560 2021-06-02 19:15:29

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,275

Re: Post your handy self made command line utilities

@lmn, nice.
I use a similar one liner no fzf but dmenu used to open whatever...

Two things, 'cd' that's ugly! there's no need at all...   'find "$dir"' unless you're really disturbed by the full path of course :lol

xargs, not all script / document / whatever names are well formed, specially downloaded ones: 'xargs -0'

Offline

#3561 2021-06-02 20:09:03

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,280

Re: Post your handy self made command line utilities

qinohe wrote:

'find "$dir"' unless you're really disturbed by the full path of course

And even if you are disturbed by that, if you are using gnu-find

find "$dir" -type f -printf %P\\n

Side note, this gets rid of the need for the sed command.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3562 2021-06-02 21:00:58

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,275

Re: Post your handy self made command line utilities

Trilby wrote:

And even if you are disturbed by that, if you are using gnu-find

find "$dir" -type f -printf %P\\n

Side note, this gets rid of the need for the sed command.

I know, and it looks nice indeed  I use it to enumerate items in some cases.

In this situation it won't work if your search is 'Documents' and you open 'Documents/pdf/awk_course.pdf'

You'll get a message the pdf doesn't exist, at least that counts when using dmenu, haven't tried fzf but I think it's the same result.

edit: used this command:

find "$dir" -type f -printf "%P\n"  | sort -n | fzf -e --reverse --height 40% --info=inline --border | xargs -r -0 xdg-open

I even installed 'fzf' to try if it's fuzzy find work in magical ways, it doesn't..  back to dmenu it is;)
BTW. it wouldn't even work if you set '-maxdepth 1'

Last edited by qinohe (2021-06-02 21:38:23)

Offline

#3563 Yesterday 18:48:21

tzihad
Member
From: Bangladesh
Registered: 2020-08-14
Posts: 2

Re: Post your handy self made command line utilities

Made a script to change yakuake/konsole theme with GNOME - Night theme Switcher  Extension

.zshrc

source /home/zihad/.required/shell_startup.sh
autoload -U add-zsh-hook
add-zsh-hook precmd konsoleswitchtheme


shell_startup.sh

#!/bin/zsh
konsoledark () {
  konsoleprofile colors=Sweet
}
konsolelight () {
  konsoleprofile colors=Light
}
konsoleswitchtheme () {
ENV_THEME=$(cat /home/zihad/.required/pipe/ENV_THEME)
  if [[ "$ENV_THEME" = *"Light"* ]]; then
      konsolelight  
  else 
      konsoledark
  fi
  # /home/zihad/.required/themequery
  # ret=$?
  # if [ $ret -ne 0 ]; then
  #     konsolelight  
  # else 
  #     konsoledark
  # fi
}
konsoleswitchtheme

dark.sh

#!/bin/bash
#/home/zihad/.required/themechanged 0
echo Dark > /home/zihad/.required/pipe/ENV_THEME

light.sh

#!/bin/bash
#/home/zihad/.required/themechanged 1
echo Light > /home/zihad/.required/pipe/ENV_THEME

Offline

#3564 Yesterday 20:18:39

qinohe
Member
From: Netherlands
Registered: 2012-06-20
Posts: 1,275

Re: Post your handy self made command line utilities

@tzihad, Not that I don't like your script but isn't it a little over the top :-)
This would do the same, put under a quick button and forget the other scripts...

konsoleswitchtheme () {
env_theme='/home/zihad/.required/pipe/ENV_THEME'
  if [[ $(<"$env_theme") != light ]]; then
      sed -i 's/dark/light/'  "$env_theme"
  else 
      sed -i 's/light/dark/'  "$env_theme"
  fi
}
konsoleswitchtheme

Offline

Board footer

Powered by FluxBB