Skip to content

Instantly share code, notes, and snippets.

@BcRikko
Created January 27, 2016 12:25
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 BcRikko/19a18c297204fecd84bc to your computer and use it in GitHub Desktop.
Save BcRikko/19a18c297204fecd84bc to your computer and use it in GitHub Desktop.
Copy__writingのフォロワー数を、5秒に1回スクレイピング
<!--
[Copy__writingのフォロワー数を、5秒に1回スクレイピング](https://gist.github.com/koron/974c9d77b541c35b0af4)
をみて、ウチも実装してみたいってことでJavaScriptで実装してみた
-->
<!doctype html>
<html lang="ja">
<head>
<title>Copy__writing</title>
</head>
<body>
<div id="app"></div>
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<script src="jquery.xdomainajax.js"></script>
<script>
uri = 'https://twitter.com/Copy__writing';
var getDate = () => {
now = new Date();
year = "" + now.getFullYear();
month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
}
setInterval(() => {
$.get(uri, (data) => {
$('#app').prepend(getDate() + ' : ' + data.responseText.match(/title="([0-9,]+) Followers"/)[1] + ' Forrows<br>');
});
}, 5000);
</script>
</body>
</html>
/**
* jQuery.ajax mid - CROSS DOMAIN AJAX
* ---
* @author James Padolsey (http://james.padolsey.com)
* @version 0.11
* @updated 12-JAN-10
* ---
* Note: Read the README!
* ---
* @info http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
*/
jQuery.ajax = (function(_ajax){
var protocol = location.protocol,
hostname = location.hostname,
exRegex = RegExp(protocol + '//' + hostname),
YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
query = 'select * from html where url="{URL}" and xpath="*"';
function isExternal(url) {
return !exRegex.test(url) && /:\/\//.test(url);
}
return function(o) {
var url = o.url;
if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {
// Manipulate options so that JSONP-x request is made to YQL
o.url = YQL;
o.dataType = 'json';
o.data = {
q: query.replace(
'{URL}',
url + (o.data ?
(/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
: '')
),
format: 'xml'
};
// Since it's a JSONP request
// complete === success
if (!o.success && o.complete) {
o.success = o.complete;
delete o.complete;
}
o.success = (function(_success){
return function(data) {
if (_success) {
// Fake XHR callback.
_success.call(this, {
responseText: (data.results[0] || '')
// YQL screws with <script>s
// Get rid of them
.replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
}, 'success');
}
};
})(o.success);
}
return _ajax.apply(this, arguments);
};
})(jQuery.ajax);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment