Created
March 26, 2013 19:54
-
-
Save ryanvalentin/5248630 to your computer and use it in GitHub Desktop.
Update all thread titles based on Query
This file contains hidden or 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 | |
date_default_timezone_set('America/Los_Angeles'); | |
$apikey = '<your disqus secret key>'; // Your Disqus secret key from http://disqus.com/api/ | |
$forum = '<your forum shortname>'; | |
$limit = '100'; // max is 100 for this endpoint. 25 is default | |
$order = 'asc'; // asc = oldest to newest. default is desc | |
$since = '1362117600'; // 1362117600 = March 1, 2013 at Midnight | |
$accessToken = '<your admin access token>'; // Get your access token from your API application page: http://disqus.com/api/applications/ | |
$endpoint = 'https://disqus.com/api/3.0/threads/list?api_secret='.$apikey.'&forum='.$forum.'&limit='.$limit.'&order='.$order.'&since='.$since; | |
$j=0; | |
list100Threads($endpoint,$cursor,$j); | |
function list100Threads($endpoint,$cursor,$j) { | |
$i = 0; | |
// create a new cURL resource | |
$session = curl_init($endpoint.$cursor); | |
// set URL and other appropriate options | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, 1); // instead of just returning true on success, return the result on success | |
// set threads info | |
$data = curl_exec($session); | |
// close cURL resource, and free up system resources | |
curl_close($session); | |
// decode the json data to make it easier to parse the php | |
$results = json_decode($data); | |
if ($results === NULL) die('Error parsing json'); | |
// grab threads | |
$threads = $results->response; | |
// grab the current cursor | |
$cursor = $results->cursor; | |
// Set the patterns you're looking for | |
$pattern = "Mobile Template: Story"; | |
foreach ($threads as $thread) | |
{ | |
// TODO get updated thread title here | |
$newtitle = "Sample title, get a real one"; | |
// Current incorrect title | |
$oldtitle = $thread->title; | |
// Thread ID used to update | |
$thread = $thread->id; | |
if ($oldtitle == $pattern) | |
{ | |
echo "Changing " . $oldtitle . " to " . $newtitle . "<br/>"; | |
/* TODO uncomment this once you have the proper new title | |
// Update the thread | |
$updateThread = "https://disqus.com/api/3.0/threads/update.json?api_secret=" . $apikey . "&thread=" . $thread . "&title=" . urlencode($newtitle) . "&access_token=" . $accessToken; | |
$updateThreadSession = curl_init($updateThread); | |
curl_setopt($updateThreadSession,CURLOPT_POST,1); | |
curl_setopt($updateThreadSession,CURLOPT_POSTFIELDS,''); | |
curl_setopt($updateThreadSession,CURLOPT_RETURNTRANSFER,1); | |
$result = curl_exec($updateThreadSession); | |
curl_close($updateThreadSession); | |
if ($result === NULL) | |
{ | |
echo "Error updating thread " . $thread . "<br />"; | |
} | |
else | |
{ | |
echo "Successfully updated thread " . $thread . "<br />"; | |
} | |
*/ | |
} | |
else | |
{ | |
echo "Skipped " . $oldtitle . "<br/>"; | |
} | |
$i++; | |
} | |
// cursor through until today | |
if ($i == 100) { | |
$cursor = "&cursor=" . $cursor->next; | |
$i = 0; | |
list100Threads($endpoint,$cursor,$j); | |
/* uncomment to only run $j number of iterations | |
$j++; | |
if ($j < 10) { | |
list100Threads($endpoint,$cursor,$j); | |
}*/ | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment