Read data from log file and put to file name

Viewed 72
#!/bin/bash

title=$(echo "$1" | sed "s/.*\///" | cut -f 1 -d '.')

function _ask() {
    while [[ $url == "" ]]; do
        echo ; echo -e "Wklej link do filmu:" ; read -e url
    done
 }
   
napi.sh search -k movie "$title" 
_ask

napi.sh subtitles "$url" > napi.log

echo Pobieram napisy:

napi.sh download -e srt `grep -o 'napiprojekt:.*' napi.log`

exit

napi.log

00:0001 - wywolano o pią, 7 maj 2021, 22:04:42 CEST
00:0002 - system: linux, forkow: 32, wersja: v2.0.0
00:0003 - Przetwarzam:  [http://napiprojekt.pl/napisy-910-Sissi-młoda-cesarzowa-(1956)]
Rozmiar:        732258304 bajtow | fps:     25 | napiprojekt:d40a1ef492e0dd094bc42141fd6e2dba
Rozmiar:        733724672 bajtow | fps:     25 | napiprojekt:38631781c8d9420eadfa13e9fe5d803b
Rozmiar:        733888512 bajtow | fps:     25 | napiprojekt:626e99378154981617418da4b39a098f
Rozmiar:        733685760 bajtow | fps:     25 | napiprojekt:9b9787e3e57754fb4301d10e865efdad
Rozmiar:        733751296 bajtow | fps:     25 | napiprojekt:f066e7a2da79ef924793212f52f0afae
Rozmiar:        855366544 bajtow | fps:     25 | napiprojekt:bbc652046e94f1802d6bea0e7d21643e
Rozmiar:       4469515020 bajtow | fps: 23.976 | napiprojekt:9470761b635b733c2befab2df62f2672
Rozmiar:        796309304 bajtow | fps:     25 | napiprojekt:a385be50baeffaad680fb03c8c0e8ede

My script and another napi.sh https://gitlab.com/hesperos/napi I use to download all subs for selected movie.

napi.sh download -e srt `grep -o 'napiprojekt:.*' napi.log`

this code help me to read all ids from log file.

napi.sh download -e srt napiprojekt:a385be50baeffaad680fb03c8c0e8ede napiprojekt:9470761b635b733c2befab2df62f2672 ...

and napi.sh save files name as id name:

d40a1ef492e0dd094bc42141fd6e2dba.srt
626e99378154981617418da4b39a098f.srt
a385be50baeffaad680fb03c8c0e8ede.srt 

Someone can help me to add to file name information about fps, this data can be received from napi.log compare id with file name and add?

Greetings.

Final script thx for help @Zilog80

#!/bin/bash
title=$(echo "${1%.*}" | sed "s/.*\///" )

function _ask() {
    while [[ $url == "" ]]; do
        echo ; echo -e "Wklej link do filmu:" ; read -e url
    done
 }
   
napi.sh search -k movie "$title" 
_ask

mkdir "$title"
cd "$title"

napi.sh subtitles "$url" > napi.log

echo Pobieram napisy:
napi.sh download -e srt `grep -o 'napiprojekt:.*' napi.log`


# Command to rename the files with the fps 
awk -F'|' '/napiprojekt:/ { fps=$2;gsub(/^[^0-9\.]*/,"", fps); gsub(/ /,"", fps);  
filename=$3; gsub(/^ *napiprojekt:/,"",filename); gsub(/ /,"\\ ",filename); 
print "mv "filename".srt FPS_"fps"_"filename".srt"}' napi.log | while read move_cmd; do 
  [ ! -z "${move_cmd}" ] && ${move_cmd}; 
done

for f in *.srt 
do
    if [ "$charset" != 'utf-8' ] 
    then
        echo Koduje do UTF-8:
        iconv -f windows-1250 -t UTF-8 "$f" -o "$f"_utf8
        mv -f "$f"_utf8 "$f"
    fi
done

echo Dodaje tytuł do nazwy:
for f in *.srt; do mv -f "$f" ../"$title"_"$f"; done

cd ..
rm -r -f "$title"

exit
1 Answers

I guess you're looking for an extract of napi.log that will return the napi project including the fps number.

Use then awk -F'\|' '/napiprojekt:/ { fps=$2;gsub(/^[^0-9\.]*/,"", fps); print $3"_"fps}' napi.log instead of grep -o 'napiprojekt:.*' napi.log :

napi.sh download -e srt `awk -F'\|' '/napiprojekt:/ { fps=$2;gsub(/^[^0-9\.]*/,"", fps);
 print $3"_"fps}' napi.log`

With your example napi.log, this will return :

 napiprojekt:d40a1ef492e0dd094bc42141fd6e2dba_25
 napiprojekt:38631781c8d9420eadfa13e9fe5d803b_25
 napiprojekt:626e99378154981617418da4b39a098f_25
 napiprojekt:9b9787e3e57754fb4301d10e865efdad_25
 napiprojekt:f066e7a2da79ef924793212f52f0afae_25
 napiprojekt:bbc652046e94f1802d6bea0e7d21643e_25
 napiprojekt:9470761b635b733c2befab2df62f2672_23.976
 napiprojekt:a385be50baeffaad680fb03c8c0e8ede_25

EDIT The problem comes from the fact that the napi will use the hash to make download and will use them as is to name the file. So you have to rename the files after the napi.sh donwload command, like with these command :

awk -F'|' '/napiprojekt:/ { fps=$2;gsub(/^[^0-9\.]*/,"", fps);gsub(/ /,"", fps);  
filename=$3; gsub(/^ *napiprojekt:/,"",filename); gsub(/ /,"\\ ",filename); 
print "mv "filename".srt "filename"_"fps".srt"}' napi.log | while read move_cmd; do 
  [ ! -z "${move_cmd}" ] && ${move_cmd}; 
done

Here is your script including that command to do that :

#!/bin/bash

title=$(echo "$1" | sed "s/.*\///" | cut -f 1 -d '.')

function _ask() {
    while [[ $url == "" ]]; do
        echo ; echo -e "Wklej link do filmu:" ; read -e url
    done
 }
   
napi.sh search -k movie "$title" 
_ask

napi.sh subtitles "$url" > napi.log

echo Pobieram napisy:

napi.sh download -e srt `grep -o 'napiprojekt:.*' napi.log`

# Command to rename the files with the fps 
awk -F'|' '/napiprojekt:/ { fps=$2;gsub(/^[^0-9\.]*/,"", fps); gsub(/ /,"", fps);  
filename=$3; gsub(/^ *napiprojekt:/,"",filename); gsub(/ /,"\\ ",filename); 
print "mv "filename".srt "filename"_"fps".srt"}' napi.log | while read move_cmd; do 
  [ ! -z "${move_cmd}" ] && ${move_cmd}; 
done

exit
Related