Skip to content

Instantly share code, notes, and snippets.

@opn
Created June 26, 2017 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opn/831717436cd2d76628c4d8d70784a8b0 to your computer and use it in GitHub Desktop.
Save opn/831717436cd2d76628c4d8d70784a8b0 to your computer and use it in GitHub Desktop.
This the is Revigniter controller 'mw_image.lc' from the domain 'rest.livecode.world'.
<?rev
# PUT YOUR HANDLER NAMES INTO THE GLOBAL gControllerHandlers AS A COMMA SEPARATED LIST
put "index" into gControllerHandlers
# THIS COMMAND WILL BE CALLED IF NONE IS SPECIFYED IN THE URI
command index
-- put "https://audioboom.com/boos/3844212-fedwiki-authoring-tool" into droppedURL
-- put "https://audioboom.com/boos/3836093-raspberrypad" into droppedURL
put $_POST_RAW into dropJSON
put json_ToArray (dropJSON) into dropArray
put dropArray ["text"] into droppedURL
put mediwiki_ExtractImageFileFromUrl (droppedURL) into shortImageFile
-- put "Andalusian_horse_moscow.jpg" into shortImageFile
put fedwiki_FetchWikiMediaPageJson (shortImageFile) into someJSON
if someJSON is empty then
put "Error, this was not an mediawiki image url" into errorText
put fedwiki_ConstructErrorJSON (errorText) into someJSON
end if
put new header "Content-Type: application/json; charset=utf-8"
put new header "Access-Control-Allow-Origin: *"
put new header "Access-Control-Allow-Headers: Accept, Authorization, Content-Type"
put new header "Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE OPTIONS, LINK, UNLINK"
put someJSON
end index
function mediwiki_ExtractImageFileFromUrl someUrl
-- https://en.wikipedia.org/wiki/Cat#/media/File:AfricanWildCat.jpg
url_Deconstruct someUrl, someProtocol, urlDomain, urlPath, shortName, fileExtension, uName, pWord
if urlDomain ends with ".wikipedia.org" is false then return empty
set the itemdelimiter to "/"
if item -1 of urlPath is not among the items of "media/wiki" then return empty
set the itemdelimiter to ":"
if item 1 of shortName is not "File" then return empty
put item 2 of shortName & "." & fileExtension into shortFile
return shortFile
end mediwiki_ExtractImageFileFromUrl
function fedwiki_FetchWikiMediaPageJson shortImageFile
put fedwiki_FetchWikiMediaPageArray (shortImageFile) into pageArray
if pageArray is an array then
put json_FromArray (pageArray) into pageJSON
return pageJSON
else
return pageArray
end if
end fedwiki_FetchWikiMediaPageJson
command url_Deconstruct someUrl, @someProtocol, @urlDomain, @urlPath, @shortName, @fileExtension, @uName, @pWord
/*
-- from http://regexlib.com/REDetails.aspx?regexp_id=628
-- put "(?:(?<protocol>http(?:s?)|ftp)(?:\:\/\/)) (?:(?<usrpwd>\w+\:\w+)(?:\@))? (?<domain>[^/\r\n\:]+)? (?<port>\:\d+)? (?<path>(?:\/.*)*\/)? (?<filename>.*?\.(?<ext>\w{2,4}))? (?<qrystr>\??(?:\w+\=[^\#]+)(?:\&?\w+\=\w+)*)* (?<bkmrk>\#.*)?" into someReg
*/
set the itemdelimiter to ":"
put item 1 of someUrl into someProtocol
if someProtocol is among the items of "file:binFile:ftp:http:https" then
put someUrl into someFile
delete item 1 of someFile
if char 1 to 2 of someFile = "//" then delete char 1 to 2 of someFile
-- just in case it has a ? param at the end
-- strip it for now
set the itemdelimiter to "?"
put item 1 of someFile into someFile
set the itemdelimiter to ":"
get offset("@", someFile)
if it = 0 then
put empty into uName
put empty into pWord
else
put char 1 to (it - 1) of someFile into authBit
repeat while char 1 of authBit is "/"
delete char 1 of authBit
end repeat
if the number of items of authBit = 2 then
put item 1 of authBit into uName
put item 2 of authBit into pWord
delete char 1 to it of someFile
else
-- "@" must be in url ignore
put empty into uName
put empty into pWord
end if
end if
file_Deconstruct someFile, someRoot, shortName, fileExtension
set the itemdelimiter to "/"
put item 1 of someRoot into UrlDomain
put item 2 to -1 of someRoot into urlPath
return true
else
put empty into someProtocol
put empty into UrlDomain
put empty into urlPath
put empty into shortName
put empty into fileExtension
put empty into uName
put empty into pWord
return false
end if
end url_Deconstruct
command file_Deconstruct someFile, @someRoot, @shortName, @fileExtension
-- was "deconstruct_File"
-- should turn someRoot into someFolder and add "/" to end
if someFile is empty then
put the effective filename of this stack into someFile
end if
put someFile into someRoot
put the itemdelimiter into originalDelim
set the itemdelimiter to "/"
put last item of someFile into shortName
delete last item of someRoot
if shortName contains "." then
set the itemdelimiter to "."
put last item of shortName into fileExtension
delete last item of shortName
else
put empty into fileExtension
end if
set the itemdelimiter to originalDelim
end file_Deconstruct
function fedwiki_ConstructErrorJSON errorText
put fedwiki_ConstructNewPageArray ("Error", errorText) into someArray
put json_FromArray (someArray) into someJSON
return someJSON
end fedwiki_ConstructErrorJSON
command fedwiki_AddToEndOfIndexArray dataOrArray, @someArray
put item 2 of the extents of someArray into indexNum
put someArray [indexNum]["id"] into afterID
add 1 to indexNum
put dataOrArray into someArray [indexNum]
return afterID -- figure out what we just added it after
end fedwiki_AddToEndOfIndexArray
command fedwiki_AddToJournalArray @journalArray, itemArray, journalType, pAfterID, pMilliseconds
if pMilliseconds is empty then put the milliseconds into pMilliseconds
if pAfterID is not empty then
put pAfterID into journalEntryArray ["after"]
end if
put pMilliseconds into journalEntryArray ["date"]
put itemArray ["id"] into journalEntryArray ["id"]
put itemArray into journalEntryArray ["item"]
put journalType into journalEntryArray ["type"]
fedwiki_AddToEndOfIndexArray journalEntryArray, journalArray
end fedwiki_AddToJournalArray
function fedwiki_ConstructFactoryArray pID
if pID is empty then
put text_Hash (the ticks & "factory") into pID
end if
put "factory" into factoryArray ["type"]
put pID into factoryArray ["id"]
return factoryArray
end fedwiki_ConstructFactoryArray
function fedwiki_ConstructImageArray someImage, imageType, pImageCaption, pID
if pID is empty then
put md5_Hash (the ticks & "image") into pID
end if
if pImageCaption is empty then put "Uploaded image" into pImageCaption
put "Uploaded image" into imageArray ["caption"]
put pID into imageArray ["id"]
put pImageCaption into imageArray ["text"]
put "image" into imageArray ["type"]
put "data:image/" & imageType & ";base64," into dataImageUrl
put base64encode (someImage) after dataImageUrl
put dataImageUrl into imageArray ["url"]
return imageArray
end fedwiki_ConstructImageArray
function fedwiki_ConstructJournalArray pageTitle
put the milliseconds into journalArray [1]["date"]
put empty into journalArray [1]["item"]["story"]
put pageTitle into journalArray [1]["item"]["title"]
put "create" into journalArray [1]["type"]
return journalArray
end fedwiki_ConstructJournalArray
function fedwiki_ConstructNewImagePageArray pageTitle, someImage, imageType, pImageCaption
local pageArray, journalArray
put pageTitle into pageArray ["title"]
put fedwiki_ConstructImageArray (someImage, imageType, pImageCaption) into imageArray
fedwiki_PageArrayAddToEnd imageArray, pageArray
put fedwiki_ConstructJournalArray (pageTitle) into journalArray
fedwiki_AddToJournalArray journalArray, imageArray, "add"
put journalArray into pageArray ["journal"]
return pageArray
end fedwiki_ConstructNewImagePageArray
function fedwiki_FetchWikiMediaPageArray shortImageFile
-- let's get information about the file
put wikimedia_FetchFileMetadata (shortImageFile) into someJson
put json_ToArray (someJson) into queryArray
put queryArray ["query"]["pages"] into pageArray
put line 1 of keys (pageArray) into pageID -- should only be one line
put pageArray [pageID]["title"] into imageTitle -- same as shortImageFile
put pageArray [pageID]["imageinfo"][1] into imageInfoArray
put imageInfoArray ["descriptionurl"] into descriptionURL
put imageInfoArray ["url"] into imageURL
put imageInfoArray ["extmetadata"] into extmetArray
put extmetArray ["Categories"]["value"] into imageCategoryIndex
replace "|" with CR in imageCategoryIndex
put extmetArray ["ImageDescription"]["value"] into imageDescription
put extmetArray ["LicenseShortName"]["value"] into licenseDescription
put imageDescription into imageCaption
put " - [" & descriptionURL && "wikimedia]" after imageCaption
put url imageURL into someImage
if someImage is empty then return ("Error, image" && imageURL && "is empty")
set the itemdelimiter to "."
put item -1 of imageURL into imageType -- bit of a hack
put fedwiki_ConstructNewImagePageArray (imageTitle, someImage, imageType, imageCaption) into pageArray
return pageArray
end fedwiki_FetchWikiMediaPageArray
command fedwiki_PageArrayAddToEnd storyItemArray, @pageArray
-- this add a factory to the journal and a itemArray to the journal and story
-- all with the same id
put pageArray ["story"] into storyArray
put pageArray ["journal"] into journalArray
put storyItemArray ["id"] into itemID
-- add the item to the end of the story
fedwiki_AddToEndOfIndexArray storyItemArray, storyArray
-- figure out what we just added it after
-- this can be empty if it is the first entry
put the result into afterID
put storyArray into pageArray ["story"]
-- 3) Now let's add the edit or add to the journal
-- make the factory have the same id as the item we will add to the story
put fedwiki_ConstructFactoryArray (itemID) into factoryArray
-- now add a journal entry for adding after the id of the end paragraph
fedwiki_AddToJournalArray journalArray, factoryArray, "add", afterID
-- finally add the journal entry for editing the factory
fedwiki_AddToJournalArray journalArray, storyItemArray, "edit"
put journalArray into pageArray ["journal"]
end fedwiki_PageArrayAddToEnd
function json_FromArray pArray, pForceRootType, pPretty
-- identical to (simply renamed) "ArrayToJSON"
-- pArray - array to be encoded
-- pForceRootType - can force the root to be an object if it looks like an array
-- pPretty - include whitespace
repeat for each key tKey in pArray
if pArray[tKey] is an array then
put "}" & json_FromArray (pArray[tKey]) into pArray[tKey]
end if
end repeat
return (mergJSONEncode ("pArray", pForceRootType, pPretty))
end json_FromArray
function json_ToArray pJSON
if pJSON is empty then return false
try -- as otherwise an error with non-json causes script to exit
local tArray,tKeys
if pJSON is empty then return empty
repeat for each line tKey in mergJSONDecode (pJSON,"tArray")
put json_ToArray (tArray[tKey]) into tArray[tKey]
end repeat
return tArray
catch e
return empty
end try
end json_ToArray
function md5_Hash someText
local hexDigest
put md5digest (someText) into someBinaryData
return binarydecode ("H*", someBinaryData, hexDigest)
return hexDigest
end md5_Hash
function sha1_Hash someData
local hexDigest
put sha1digest (someData) into someBinaryData
get binarydecode ("H*", someBinaryData, hexDigest)
return hexDigest
end sha1_Hash
function text_Hash someData, pWhichDigest
if pWhichDigest = "sha1" then
return sha1_Hash (someData)
else
return md5_Hash (someData)
end if
end text_Hash
function wikimedia_FetchFileMetadata shortImageFile, pExtended
/*
-- ?action=query&titles=Image:Commons-logo.svg&prop=imageinfo&iiprop=metadata&iimetadataversion=latest
Raw metadata that is embedded in file. More information on the format
-- ?action=query&titles=Image:Commons-logo.svg&prop=imageinfo&iiprop=extmetadata
Includes metadata from file page
*/
if pExtended is false then
put wikimedia_RestfulFetch (shortImageFile, "imageinfo", "iiprop=metadata|url&iimetadataversion=latest") into someJSON
else
put wikimedia_RestfulFetch (shortImageFile, "imageinfo", "iiprop=extmetadata|url") into someJSON
end if
return someJSON
end wikimedia_FetchFileMetadata
function wikimedia_GetApiRoot
return "https://commons.wikimedia.org/w/api.php"
end wikimedia_GetApiRoot
function wikimedia_RestfulFetch shortImageFile, pProps, pExtraProps, pFormat
if pFormat is empty then put "json" into pFormat
get merge ("?action=query&titles=Image:[[shortImageFile]]&prop=[[pProps]]&format=[[pFormat]]")
if pExtraProps is not empty then put "&" & pExtraProps after it
put wikimedia_GetApiRoot() & it into someURL
if the environment = "server" then
-- should set user agent headers with curl here as well
put "curl" && quote & someURL & quote into someShell
put shell(someShell) into someJSON
delete line 1 to 4 of someJSON
else
wikipedia_SetUserAgent
put url someURL into someJSON
end if
if the number of words of someJSON = 0 then
return someURL
else
return someJSON
end if
end wikimedia_RestfulFetch
command wikipedia_SetUserAgent
-- https://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client
put "User-Agent: WikiApp/0.1 (http://livecode.viral.academy/wikiapp.html; david@viral.academy) MacOS Livecode/7.06" into userHeader
set the httpheaders to userHeader
end wikipedia_SetUserAgent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment