randomfox (randomfox) wrote,
randomfox
randomfox

K-Meleon Pager macros for Flickr, Google, and Livejournal

In accel.cfg:
CTRL VK_NEXT = luamacros(PageNext)
CTRL VK_PRIOR = luamacros(PagePrev) 
In toolbars.cfg:
Pages{
	PreviousPage{
	luamacros(PagePrev)
	Previous page
	sysmenu.bmp[5]
	}

	NextPage{
	luamacros(PageNext)
	Next page
	sysmenu.bmp[4]
	}
}
Lua code:
#!lua

--[[
Macros for paging backwards and forwards in Flickr photostreams, etc.
Use luamacros(PagePrev) and luamacros(PageNext) in menu and accelerator
definitions.
]]

local function prevnext_flickr(url)
    local pagematches = {
	{ 
	    pat = "/page(%d+)/?$", 
	    f = function (n) 
		return n <= 1 and "/" or "/page" .. n .. "/" 
	    end 
	},
	{
	    pat = "/%?page=(%d+)/?$",
	    f = function (n)
		return n <= 1 and "/" or "/?page=" .. n
	    end
	},
	{
	    pat = "/%?page=(%d+)&filter=default/?$",
	    f = function (n)
		return n <= 1 and "/" or "/?page=" .. n .. "&filter=default"
	    end
	},
	{
	    pat = "%?page=(%d+)/?$",
	    f = function (n)
		return n <= 1 and "" or "?page=" .. n
	    end
	},
	{
	    pat = "&page=(%d+)/?$",
	    f = function (n)
		return n <= 1 and "" or "&page=" .. n
	    end
	},
    }

    for _, pagematch in ipairs(pagematches) do
	local locb, _, pagenum = string.find(url, pagematch.pat)
	if locb then
	    return string.gsub(url, pagematch.pat, pagematch.f(pagenum - 1)),
		string.gsub(url, pagematch.pat, pagematch.f(pagenum + 1))
	end
    end

    local baseurls = {
	{
	    p2 = "/page2/",
	    urls = {
		"flickr%.com/photos/[^/]+/?$",
		"flickr%.com/photos/[^/]+/popular%-views/?$",
		"flickr%.com/photos/[^/]+/popular%-faves/?$",
		"flickr%.com/photos/[^/]+/popular%-comments/?$",
		"flickr%.com/photos/[^/]+/popular%-interesting/?$",
		"flickr%.com/photos/[^/]+/tags/[^/]+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/%d+/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/%d+/%d+/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/detail/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/%d+/detail/?$",
		"flickr%.com/photos/[^/]+/archives/date%-taken/%d+/%d+/%d+/detail/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/detail/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/%d+/detail/?$",
		"flickr%.com/photos/[^/]+/archives/date%-posted/%d+/%d+/%d+/detail/?$",
		"flickr%.com/photos/friends/?$",
		"flickr%.com/groups/[^/]+/pool/?$",
		"flickr%.com/groups/[^/]+/pool/[^/]+/?$",
		"flickr%.com/groups/[^/]+/discuss/?$",
		"flickr%.com/groups/[^/]+/discuss/%d+/?$",
	    }
	},
	{
	    p2 = "/?page=2",
	    urls = {
		"flickr%.com/photos/tags/[^/]+/?$",
	    }
	},
	{
	    p2 = "/?page=2&filter=default",
	    urls = {
		"flickr%.com/people/[^/]+/contacts/?$",
	    }
	},
	{
	    p2 = "?page=2",
	    urls = {
		"flickr%.com/photos_comments%.gne/?$",
	    }
	},
	{
	    p2 = "&page=2",
	    urls = {
		"flickr%.com/recent_activity%.gne%?days=[^&]+/?$",
	    }
	},
    }

    local no_endslash = string.gsub(string.gsub(url, "%?saved=1$", ""), "/?$", "")
    for _, baseurl in ipairs(baseurls) do
	for _, pat in ipairs(baseurl.urls) do
	    if string.find(no_endslash, pat) then	
		return url, no_endslash .. baseurl.p2
	    end
	end
    end

    return url, url
end

local function prevnext_lj(url)
    local skipmatch = "%?skip=(%d+)/?$"

    local locb, _, pagenum = string.find(url, skipmatch)
    if locb then
	local function skipstr(n) return n <= 0 and "" or "?skip=" .. n end
	return string.gsub(url, skipmatch, skipstr(pagenum - 15)),
	    string.gsub(url, skipmatch, skipstr(pagenum + 15))
    end

    local no_endslash = string.gsub(url, "/?$", "")

    if string.find(url, "livejournal%.com/?$") then
	return url, no_endslash .. "/?skip=15"
    end

    if string.find(url, "livejournal%.com/friends/?$") or string.find(url, "livejournal%.com/friends/[^/]+/?$") then
	return url, no_endslash .. "?skip=15"
    end

    return url, url
end

local function prevnext_google(url)
    local skipmatch = "&start=(%d+)"

    local locb, _, pagenum = string.find(url, skipmatch)
    if locb then
	local function skipstr(n) return n <= 0 and "" or "&start=" .. n end
	return string.gsub(url, skipmatch, skipstr(pagenum - 10)),
	    string.gsub(url, skipmatch, skipstr(pagenum + 10))
    end

    local no_endslash = string.gsub(url, "/?$", "")

    if string.find(url, "google%.com/search%?") then
	return url, no_endslash .. "&start=10"
    end

    return url, url
end

local function prevnext(url)
    local ftable = {
	{ pat = "^http://www%.flickr%.com", f = prevnext_flickr },
	{ pat = "^http://flickr%.com", f = prevnext_flickr },
	{ pat = "^http://[^/%.]+%.livejournal%.com", f = prevnext_lj },
	{ pat = "^http://www%.google%.com", f = prevnext_google },
	{ pat = "^http://google%.com", f = prevnext_google },
    }

    for _, fpair in ipairs(ftable) do
	if string.find(url, fpair.pat) then
	    return fpair.f(url)
	end
    end

    return url, url
end

function PagePrev()
    local _, url = GetDocInfo()
    local prevurl, _ = prevnext(url)
    NavigateTo(prevurl, OPEN)
end

function PageNext()
    local _, url = GetDocInfo()
    local _, nexturl = prevnext(url)
    NavigateTo(nexturl, OPEN)
end

--[[
function testnp(url)
    local prevurl, nexturl = prevnext(url)
    print("url = " .. url)
    print("prev = " .. prevurl)
    print("next = " .. nexturl)
    print()
end

testnp("http://www.flickr.com/photos/mortonfox/")
testnp("http://www.flickr.com/photos/mortonfox/page2")
testnp("http://www.flickr.com/photos/mortonfox/page3/")
--]]

-- Last updated: May 16, 2006
-- The End

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments