Last active
December 25, 2015 18:59
-
-
Save AleksandrT/7024873 to your computer and use it in GitHub Desktop.
Wiki upload bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
error_reporting(-1); | |
$action = urlencode('login'); | |
$url = 'http://commons.wikimedia.org/w/api.php?'; | |
$params = 'action='. $action .'&format=php&lgname=&lgpassword='; | |
$ua = 'SpreadthesignBot/0.2 (http://www.spreadthesign.com/; info@spreadthesign.com)'; | |
$cs = '/tmp/wikibot_cookies.tmp'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt($ch, CURLOPT_USERAGENT, $ua); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 15); | |
curl_setopt($ch, CURLOPT_COOKIEFILE, $cs); | |
curl_setopt($ch, CURLOPT_COOKIEJAR, $cs); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_COOKIESESSION, true); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_VERBOSE, false); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_POST, count($params)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$r = curl_exec($ch); | |
$result = ds($r,$ch); | |
$token = $result["login"]["token"]; | |
$params = $params.'&lgtoken='.$token; | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); | |
$r = curl_exec($ch); | |
$result = ds($r, $ch); | |
if (strtolower($result['login']['result']) != 'success') { | |
die('Login failed. ' . print_r($result, 1)); | |
} | |
$query = "action=query&prop=info|revisions&titles=Foo&intoken=edit&format=txt"; | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $query); | |
$t = curl_exec($ch); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
$start = strpos($t, "[edittoken] => ") + strlen("[edittoken] => "); | |
$tok1 = substr($t, $start); | |
$r = strlen($tok1)-strlen(strrchr($tok1, "+"))+2; | |
$tokens= str_split($tok1, $r); | |
$token = $tokens[0]; | |
require_once 'inc/Config.php'; | |
$config = new Config(); | |
$c = $config -> getConfig(); | |
require_once 'inc/dblib.php'; | |
$basePath = $c["VIDEO_PATH"]; | |
$s = "SELECT v.ID, d.cID, d.Word, DATE( v.Timestamp ) AS Created, c.SLC, dm.Definition AS Description,st.Translation AS WordLanguage | |
FROM Video v | |
INNER JOIN DictionaryVideo dv ON ( dv.vID = v.ID ) | |
INNER JOIN Dictionary d ON ( d.ID = dv.dID ) | |
INNER JOIN Country c ON ( c.ID = d.cID AND c.Enabled = 1) | |
INNER JOIN DictionaryMagnet dm ON ( dm.ID = d.mID ) | |
INNER JOIN SiteTranslationMagnet stm ON ( stm.Reference = CONCAT( c.CC, '_LANG' )) | |
INNER JOIN SiteTranslation st ON ( st.mID = stm.ID AND st.cID = d.cID ) | |
WHERE d.cID =".MASTER_CID." AND v.WikiCommons IS NULL AND v.ID = 9994 | |
LIMIT 0,1"; | |
$q = mysql_query($s); | |
while ($row = mysql_fetch_assoc($q)) { | |
$vid = $row['ID']; | |
$cid = $row['cID']; | |
$word = $row['Word']; | |
$desc = $row['Description']; | |
$date = $row['Created']; | |
$language = $row['WordLanguage']; | |
$slc = $row['SLC']; | |
$text = <<<"EOT" | |
=={{int:filedesc}}== | |
{{Information | |
|description={{ $language|1=$desc}} | |
|date=$date | |
|source={{STS-link|$vid}} | |
|author={{Spread the sign}} | |
|permission={{STS-cooperation}} | |
|other_versions= | |
|other_fields= | |
}} | |
[[category:$language sign language]] | |
[[category:Videos of sign language in $language]] | |
EOT; | |
// Upload to wiki | |
$uparams = urlencode("action=upload&format=php&filename=$slc-$word-spreadthesign-$vid.ogv&url=http://media.spreadthesign.com/video/wiki/$cid/$vid.ogv&text=$text&token=$token"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $uparams); | |
$result = curl_exec($ch); | |
// Check response status | |
if (!isset($result['upload']['result']) || $result['upload']['result'] != 'Success') { | |
die("Failed to upload video ID: $vid\n" . print_r($result, 1)); | |
} | |
//Persist wiki filename | |
$safe = Utils::safe($result['upload']['filename']); | |
//mysql_query("UPDATE Video SET WikiCommons = '$safe' WHERE ID = $vid LIMIT 1"); | |
$url = $result['upload']['imageinfo']['descriptionurl']; | |
$name = $result['upload']['filename']; | |
echo "<a href=\"$url\">$name</a>\n"; | |
} | |
curl_close($ch); | |
function ds($r, $ch) { | |
$header_array = explode("Connection: close", $r); | |
$response =unserialize($header_array[1]); | |
if ($response === false) { | |
curl_setopt($ch, CURLOPT_HEADER, false); | |
$response = unserialize(curl_exec($ch)); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
} | |
return $response; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment