Applescript: как определить, какая дата новее между 2 датами (одна с веб-сайта, другая - текущая дата), но только с использованием месяца и года - PullRequest
0 голосов
/ 26 апреля 2020

Может ли кто-нибудь помочь мне определить, какая дата является более новой между двумя датами (одна с веб-сайта, другая - текущая дата), но только с использованием месяца и года

Цель:

Чтобы получить год из первого коми c, указанного на веб-сайте https://leagueofcomicgeeks.com/comics/series/117056/a-force или https://leagueofcomicgeeks.com/comics/series/124035/aquaman или его вариантов (этот второй вариант Я стремлюсь к "TBD") например. 14 октября 2015 или TBD. И сравнить его с (current date), используя только месяц и год, чтобы выяснить, какой из них является более новым, а затем предоставить результат «Текущий», если дата (с веб-сайта) новее, или TBD, или « Завершено ", если дата старше.

Сценарии:

Первый - оригинальный скрипт, который работал с этим сайтом (https://comicvine.gamespot.com/a-force/4050-82099/) а затем обновленный скрипт, созданный для работы с новым веб-сайтом (https://leagueofcomicgeeks.com/comics/series/117056/a-force или https://leagueofcomicgeeks.com/comics/series/124035/aquaman или его вариант)

Оригинал Сценарий:

use framework "Foundation"
use scripting additions

tell application "Google Chrome"
    repeat with x from 0 to ((execute active tab of first window javascript "document.getElementsByTagName('p').length") - 1)
        set theEnd to execute active tab of first window javascript "document.getElementsByTagName('p')[" & x & "].outerHTML"
        if theEnd contains "issue-date" then exit repeat
    end repeat
    set theEnd to execute active tab of first window javascript "document.getElementsByTagName('p')[" & x & "].innerHTML"
    set e to my strToDate(theEnd)
    set issueDate to (current date)
    if e is greater than or equal to issueDate then set theResult to "Current"
    if e is less than issueDate then set theResult to "Ended"
end tell

on strToDate(thisString)
    if (offset of "," in thisString) = 0 then -- no day, so add one
        set here to (offset of space in thisString)
        tell thisString to set thisString to text 1 thru here & "1," & text here thru -1
    end if
    tell current application
        set m to its ((NSDataDetector's dataDetectorWithTypes:(its NSTextCheckingTypeDate) |error|:(missing value))'s matchesInString:thisString options:0 range:{0, length of thisString})
        if (count m) > 0 then
            set d to (item 1 of m)'s |date|() -- get the NSDate of the first item
            return d as date
        end if
    end tell
    return "" -- no match in this string
end strToDate

Новый сценарий:

--The Site (League of ComicGeeks)
tell application "Google Chrome"
    repeat with x from 0 to ((execute active tab of first window javascript "document.getElementsByTagName('html').length") - 1)
        set theSite to execute active tab of first window javascript "document.getElementsByTagName('html')[" & x & "].outerHTML"
        if theSite contains "</body>" then exit repeat
    end repeat
    set theSite to execute active tab of first window javascript "document.getElementsByTagName('html')[" & x & "].innerHTML"
end tell

set theDate to extractBetween(theSite, "<ul class=\"media-list\">", "<div class=\"comic-description\">")
set theDate to extractBetween(theDate, "> &nbsp;·&nbsp; ", " &nbsp;·&nbsp;")
set the theDate to replace_chars(theDate, {"st", "nd", "rd", "th"}, "")

set monthStr to month of (current date) as text
set yearStr to year of (current date) as text
set dayStr to day of (current date) as text

if theDate is equal to "TBD" then set CorE to "Current"
if theDate is not equal to "TBD" then
    set monthDate to first word of theDate
    set yearDate to last word of theDate
    set dayDate to word -2 of theDate
    if yearDate is greater than or equal to yearStr then
        if monthDate is greater than or equal to monthStr then
            if dayDate is greater than or equal to datStr then
                set CorE to "Current"
            end if
        end if
    end if
else
    set CorE to "Ended"
end if

return CorE

on replace_chars(this_text, search_string, replacement_string)
    set AppleScript's text item delimiters to the search_string
    set the item_list to every text item of this_text
    set AppleScript's text item delimiters to the replacement_string
    set this_text to the item_list as string
    set AppleScript's text item delimiters to ""
    return this_text
end replace_chars

to extractBetween(SearchText, startText, endText)
    set tid to AppleScript's text item delimiters -- save them for later.
    set AppleScript's text item delimiters to startText -- find the first one.
    set endItems to text of text item -1 of SearchText -- everything after the first.
    set AppleScript's text item delimiters to endText -- find the end one.
    set beginningToEnd to text of text item 1 of endItems -- get the first part.
    set AppleScript's text item delimiters to tid -- back to original values.
    return beginningToEnd -- pass back the piece.
end extractBetween

Финал:

• Получить дату, или «TBD», с веб-сайта и сравните ее с (current date)

• Из результата верните либо «Текущее», большее или равное текущей дате, либо результат TBD, либо возвращение «Завершено». "для всего остального

Благодарности:

Я хотел бы отдать часть сценария on strToDate(thisString) пользователю на другом э-э форум, но это сообщение было удалено. Итак, спасибо им за помощь.

Заранее благодарю за любую помощь, которую вы можете предложить

l0g0p7

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...