Skip to content

Instantly share code, notes, and snippets.

@MihaiCraciun
Created December 22, 2014 12:50
Show Gist options
  • Save MihaiCraciun/78f0a53b7a99587d178b to your computer and use it in GitHub Desktop.
Save MihaiCraciun/78f0a53b7a99587d178b to your computer and use it in GitHub Desktop.
Simple browser monitoring interface for scheduling and viewing Scrapy spider status HTML & jQuery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple scrapyd web manager</title>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
// Scrapyd URL, i.e. http://localhost:6800/
var scrapyd_url = 'http://localhost:6800/';
// Your scrapyd project name
var project_name = 'new_project';
var all_spiders = new Array();
var running_spiders = new Array();
var finished_spiders = new Array();
function startSpider(spider_name) {
console.log('START spider clicked !');
//spider_name = $(this).attr('rel');
console.log('Spider name : '+spider_name);
$.post(scrapyd_url + 'schedule.json', {
project : project_name,
spider : spider_name
});
}
function stopSpider(job_id) {
console.log('STOP spider clicked!')
//job_id = $(this).attr('rel');
console.log('Job ID : '+job_id);
$.post(scrapyd_url + 'cancel.json', {
project : project_name,
job : job_id
});
}
$(document).ready(function() {
function listjobs() {
$.getJSON(scrapyd_url + 'listjobs.json?project=' + project_name, function(data) {
var finished_items = [];
$.each(data.finished, function(key, val) {
finished_spiders.push(val.spider);
finished_items.push('<li>' + val.spider + '&nbsp;&nbsp;<a href="' + scrapyd_url + 'logs/' + project_name + '/' + val.spider + '/' + val.id + '.log">log</a></li>');
});
$('#finished_spiders').append(finished_items.join(''));
if (data.running.length > 0) {
var running_items = [];
$.each(data.running, function(key, val) {
running_spiders.push(val.spider);
running_items.push('<li>' + val.spider + '&nbsp;&nbsp;<a href="' + scrapyd_url + 'logs/' + project_name + '/' + val.spider + '/' + val.id + '.log">log</a>&nbsp;&nbsp|&nbsp;&nbsp<span class="stop_spider" onclick="stopSpider(\'' + val.id + '\')">Stop</span></li>');
});
$('#running_spiders').append(running_items.join(''));
} else {
$('#running_spiders').append('<li>No Running spiders</li>');
}
});
}
listjobs();
// Get spiders list
$.getJSON(scrapyd_url + 'listspiders.json?project=' + project_name, function(data) {
var items = [];
//console.log(data);
$.each(data.spiders, function(key, val) {
items.push('<li id="' + val + '">' + val + '&nbsp;&nbsp;<span class="start_spider" onclick="startSpider(\''+ val + '\')">Start</span></li>');
});
$('#available_spiders').append(items.join(''));
});
});
</script>
</head>
<body>
<a href="#" onClick="window.location.reload">refresh</a>
<br />
List of running spiders
<ul id="running_spiders"></ul>
List of finished spiders (First is latest)
<ul id="finished_spiders"></ul>
List of available spiders
<ul id="available_spiders"></ul>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment