I have automated the process, but the code is long.
Register to use the API
To have write permissions at SE one needs to register an APP with SE API to authenticate. Assume you have the keys stored as
apireg = <|"client_id" -> "1224", "key" -> "keystring",
"access_token" -> "accesstokenstring"|>
Search for questions
page = 1;
searchoutput = Monitor[
Module[{more = True, url, partial, full = <|"items" -> {}|>},
url[p_] := URLBuild[
{"https://api.stackexchange.com", "2.2", "search", "advanced"}
, {
"page" -> p
, "order" -> "desc"
, "sort" -> "activity"
, "user" -> "21532"
, "url" -> "*.bkt.clouddn.com/*"
, "site" -> "mathematica"
, "filter" -> "!-MOiNm40Dv9qWI4dBqjO4HV_ySVbpgQHr"
, "key" -> Query["key"][apireg]
}];
While[more,
partial = Import[url[page], "RawJSON"];
more = partial[["has_more"]];
page += 1;
full = <|partial,
"items" -> Join[full["items"], partial["items"]]|>;
Pause[1];
];
full
], page];
questionsToEdit = Query["items", All, "question_id"]@searchoutput
Get questions, parse URL and upload image
Needs["SETools`SEUploader`"]
ClearAll[GetSEQuestion,GetURLFromSEMarkdown,moveToImgur];
GetSEQuestion[MSEQuestionId_Integer, site_String: "mathematica"] :=
Import[URLBuild[{"https://api.stackexchange.com", "2.2", "questions",
ToString[MSEQuestionId]}, {"site" -> site,
"filter" -> "!gB5*Xef0PUpwu*lEaRiDqomj.*3J9zCGB5u"}], "RawJSON"]
GetURLFromSEMarkdown[question_String] :=
StringCases[question,
Shortest[""] :>
StringJoin["http://", url]]
moveToImgur[url_String] :=
moveToImgur[url] = SETools`SEUploader`Private`stackImage[Import[url]]
Edit the question
ClearAll[FixSEQuestion];
FixSEQuestion[id_] :=
Module[{question, oldbody, questionmarkup, urlList, replaceRule,
fixedQuestion, response},
question = GetSEQuestion[id];
oldbody =
StringReplace[Query["items", 1, "body_markdown"][question],
Shortest[StringExpression["&", c__, ";"]] :>
ImportString[StringJoin["&", c, ";"], "HTML"]];
urlList = GetURLFromSEMarkdown[oldbody];
If[Length[urlList] >= 1,
replaceRule =
Echo@Map[
Rule[#, StringReplace[moveToImgur[#],
"http://" -> "https://"]] &, urlList];
fixedQuestion = StringReplace[oldbody, replaceRule];
response = URLRead@HTTPRequest[
URLBuild[{"https://api.stackexchange.com", "2.2", "questions",
ToString[id], "edit"}]
, <|"Body" -> {
"site" -> "mathematica"
, "filter" -> "!b0OfN_Di4E09vY"
, "key" -> apireg["key"]
, "access_token" -> apireg["access_token"]
,
"title" ->
StringReplace[Query["items", 1, "title"][question],
Shortest[StringExpression["&", c__, ";"]] :>
ImportString[StringJoin["&", c, ";"], "HTML"]]
,
"tags" ->
StringReplace[
ToString[Query["items", 1, "tags"][question]], {"{" -> "",
"}" -> "", ", " -> ";"}]
, "body" -> fixedQuestion
}
, "Scheme" -> "https"
, Method -> "POST"
|>];
Echo[response[{"StatusCode", "StatusCodeDescription"}]];
Import[response, {"gzip", "RawJSON"}]
]
]