It is currently October 4th, 2024, 12:43 am

Reading a specific line from a text file and changing the content of that line

Discuss the use of Lua in Script measures.
User avatar
tass_co
Posts: 523
Joined: May 4th, 2020, 3:01 pm
Location: Ankara, TURKEY

Reading a specific line from a text file and changing the content of that line

Post by tass_co »

Hello everyone.

I have a TV show reminder skin.
I am reading data from txt file with webparser plugin.

I want to automatically change series dates, episode numbers, using lua script.

Text Line content pattern

Code: Select all

@@The Mandalorian@@S03E03@@15.03.23@@https://www.imdb.com/title/tt8111088/episodes?season=3@@https://proxyrarbg.org/torrents.php?search=tt8111088@@https://turkcealtyazi.org/mov/8111088/
ps: MeasureNames=> MeaName1 - MeaEp1 - MeaDate1 - MeaImdb1 - MeaSearch1 - MeaSub1

The places I want to change are the dates and episode numbers.

S03E04 and 22.03.23
(Episode number will increase by 1 and day will increase by 7 (not to exceed 30 and 31))

I want to do this separately for the first 3 rows but without success.

Since I don't know how to use Lua script, I could only add a new line to the text file. Without being able to change the date and episode numbers, of course.

Code: Select all

function Initialize()

end

function Edit1()
	MeaGetName = SKIN:GetMeasure('MeaName1')
	MeaGetEpisode = SKIN:GetMeasure('MeaEp1')
	MeaGetDate = SKIN:GetMeasure('MeaDate1')
	MeaGetIMDB = SKIN:GetMeasure('MeaImdb1')
	MeaGetSearch = SKIN:GetMeasure('MeaSearch1')
	MeaGetSubtitle = SKIN:GetMeasure('MeaSub1')
	
	MeaGetName1 = MeaGetName:GetStringValue()
	MeaGetEpisode1 = MeaGetEpisode:GetStringValue()
	MeaGetDate1 = MeaGetDate:GetStringValue()
	MeaGetIMDB1 = MeaGetIMDB:GetStringValue()
	MeaGetSearch1 = MeaGetSearch:GetStringValue()
	MeaGetSubtitle1 = MeaGetSubtitle:GetStringValue()
	
	local FileName = SKIN:GetVariable('Note')
	FilePath = SKIN:MakePathAbsolute(FileName)
	local file = io.open(FilePath, 'a')
	file:write('@@1',MeaGetName1, '@@',MeaGetEpisode1, '@@',MeaGetDate1,'@@',MeaGetIMDB1, '@@',MeaGetSearch1, '@@',MeaGetSubtitle1)
	file:close()
end

function Edit2()
	MeaGetName = SKIN:GetMeasure('MeaName2')
	MeaGetEpisode = SKIN:GetMeasure('MeaEp2')
	MeaGetDate = SKIN:GetMeasure('MeaDate2')
	MeaGetIMDB = SKIN:GetMeasure('MeaImdb2')
	MeaGetSearch = SKIN:GetMeasure('MeaSearch2')
	MeaGetSubtitle = SKIN:GetMeasure('MeaSub2')
	
	MeaGetName1 = MeaGetName:GetStringValue()
	MeaGetEpisode1 = MeaGetEpisode:GetStringValue()
	MeaGetDate1 = MeaGetDate:GetStringValue()
	MeaGetIMDB1 = MeaGetIMDB:GetStringValue()
	MeaGetSearch1 = MeaGetSearch:GetStringValue()
	MeaGetSubtitle1 = MeaGetSubtitle:GetStringValue()
	
	local FileName = SKIN:GetVariable('Note')
	FilePath = SKIN:MakePathAbsolute(FileName)
	local file = io.open(FilePath, 'a')
	file:write('@@2',MeaGetName1, '@@',MeaGetEpisode1, '@@',MeaGetDate1,'@@',MeaGetIMDB1, '@@',MeaGetSearch1, '@@',MeaGetSubtitle1)
	file:close()
end

function Edit3()
	MeaGetName = SKIN:GetMeasure('MeaName3')
	MeaGetEpisode = SKIN:GetMeasure('MeaEp3')
	MeaGetDate = SKIN:GetMeasure('MeaDate3')
	MeaGetIMDB = SKIN:GetMeasure('MeaImdb3')
	MeaGetSearch = SKIN:GetMeasure('MeaSearch3')
	MeaGetSubtitle = SKIN:GetMeasure('MeaSub3')
	
	MeaGetName1 = MeaGetName:GetStringValue()
	MeaGetEpisode1 = MeaGetEpisode:GetStringValue()
	MeaGetDate1 = MeaGetDate:GetStringValue()
	MeaGetIMDB1 = MeaGetIMDB:GetStringValue()
	MeaGetSearch1 = MeaGetSearch:GetStringValue()
	MeaGetSubtitle1 = MeaGetSubtitle:GetStringValue()
	
	local FileName = SKIN:GetVariable('Note')
	FilePath = SKIN:MakePathAbsolute(FileName)
	local file = io.open(FilePath, 'a')
	file:write('@@3',MeaGetName1, '@@',MeaGetEpisode1, '@@',MeaGetDate1,'@@',MeaGetIMDB1, '@@',MeaGetSearch1, '@@',MeaGetSubtitle1)
	file:close()
end
I would appreciate if you could help me improve the code :bow:
I don't know where i going from here, but i promise it won't be boring... :great:
User avatar
nek
Posts: 116
Joined: November 3rd, 2019, 12:00 am

Re: Reading a specific line from a text file and changing the content of that line

Post by nek »

I am learning Lua script too.
tass_co wrote: March 15th, 2023, 1:42 am S03E04 and 22.03.23
(Episode number will increase by 1 and day will increase by 7 (not to exceed 30 and 31))
Some code for the day part. Hope it helps.

Code: Select all

--
-- http://lua-users.org/wiki/SplitJoin
-- @param sep {string}  -  separator
-- @return    {table}   -  Lua table index start at `1` not `0`
--
function string:split(sep)
  local sep, fields = sep or ":", {}
  local pattern = string.format("([^%s]+)", sep)
  self:gsub(pattern, function(c) fields[#fields+1] = c end)
  return fields
end

--
-- @param   inc   {number}  -  Number of days to add
-- @param   sdate {string}  -  '27.03.23' (DD.MM.YY), '27.03.2023' (DD.MM.YYYY)
-- @example                 -  add_day(7, '27.03.23')  add_day(7, '27/03/2023')  add_day(3)
-- @see     Dealing With Date Time | http://lua-users.org/wiki/DealingWithDateTime
-- @see     Numbers Coercion | http://lua-users.org/wiki/NumbersTutorial
--
function add_day(inc, sdate)
  if 'string'~=type(sdate) then sdate = '' end
  local now   = os.time()
  local sep   = string.match(sdate, '%p') or '/'
  local tdate, fdate = sdate:split(sep)
  local day   = tonumber(tdate[1]) or os.date('%d',now)
  local month = tonumber(tdate[2]) or os.date('%m',now)
  local year  = tonumber(tdate[3]) or os.date('%Y',now)
  if (year-0)<100 then
    year  = year + os.date('%Y',now) - os.date('%y',now)
    -- fdate = table.concat({'%d', '%m', '%y'}, sep)
    fdate = string.format('%%d%s%%m%s%%y', sep, sep)
  else
    -- fdate = table.concat({'%d', '%m', '%Y'}, sep)
    fdate = string.format('%%d%s%%m%s%%Y', sep, sep)
  end
  return os.date(fdate, os.time{year=year, month=month, day=day+inc})
end

print(add_day(7, '27.03.23'))    -- output `03.04.23`
print(add_day(7, '27/03/2023'))  -- output `03/04/2023`
print(add_day(3))                -- output add 3 days to today `DD/MM/YYYY`


--[=====[

 # Date and Time format

 YYYY MM DD  -  East Asia (ISO 8601, YYYY-MM-DD)
 DD MM YYYY  -  Europe, West, Central, and Southern Africa, South America
 MM DD YYYY  -  U.S.

 > Date format by country | https://en.wikipedia.org/wiki/Date_format_by_country
 > ISO 8601 (international standard) | https://en.wikipedia.org/wiki/ISO_8601

--]=====]
Time format code
%y year (nn)
%Y year (yyyy)
%m month (01..12)
%d day of month (01..31)

Search code | http://lua-users.org/wiki/FindPage
Online Lua | https://www.tutorialspoint.com/execute_lua_online.php
Lua Demo | https://www.lua.org/cgi-bin/demo
Date format by country | https://en.wikipedia.org/wiki/Date_format_by_country
ISO 8601 (international standard) | https://en.wikipedia.org/wiki/ISO_8601
Last edited by nek on April 6th, 2023, 7:58 am, edited 8 times in total.
User avatar
tass_co
Posts: 523
Joined: May 4th, 2020, 3:01 pm
Location: Ankara, TURKEY

Re: Reading a specific line from a text file and changing the content of that line

Post by tass_co »

nek wrote: March 15th, 2023, 5:30 am I am learning Lua script too.


Some code for the day part. Hope it helps.

Code: Select all

-- http://lua-users.org/wiki/SplitJoin
function string:split(sep)
   local sep, fields = sep or ":", {}
   local pattern = string.format("([^%s]+)", sep)
   self:gsub(pattern, function(c) fields[#fields+1] = c end)
   return fields
end

function test()

  local sdate = '27.03.23'
  local tdate = sdate:split('.')
  local year, month, day = tonumber(tdate[3]), tonumber(tdate[2]), tonumber(tdate[1])
  year = year<2000 and year + 2000 or year

  -- +7 days  | output `03.04.23` | http://lua-users.org/wiki/DealingWithDateTime
  print(os.date('%d.%m.%y', os.time{ year=year, month=month, day=day+7 }))

end
Time format code
%y year (nn)
%Y year (yyyy)
%m month (01..12)
%d day of month (01..31)

Search code | http://lua-users.org/wiki/FindPage
Online Lua | https://www.tutorialspoint.com/execute_lua_online.php
Thank you nek :bow: I will try and let you know the result :thumbup:
I don't know where i going from here, but i promise it won't be boring... :great: