Created
May 19, 2018 10:56
-
-
Save Gim6626/108a66513f039bfb5342a9fcd95994e2 to your computer and use it in GitHub Desktop.
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
/* | |
Youtube video views | |
This contract keeps in storage a views counter | |
for a given Youtube video. | |
*/ | |
pragma solidity ^0.4.0; | |
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol"; | |
interface OracleDataReceiver { | |
function __oracle_callback(string result); | |
} | |
contract YoutubeViewsOracle is usingOraclize { | |
string internal apiKeyEncrypted; | |
mapping(bytes32 => OracleDataReceiver) internal clients; | |
event newOraclizeQuery(string description); | |
event newYoutubeViewsCount(string views); | |
function YoutubeViewsOracle() payable public { | |
// See https://docs.oraclize.it/#ethereum-advanced-topics-encrypted-queries | |
// Note: Should be regenerated using format '{"key": "GOOGLE_API_KEY"}' at each contract deployment | |
apiKeyEncrypted = 'BHoLyw7hZYMXJykgkU/+/J0W4HgTGzb9CsdCzp342NTnIj4OZGgIB8HmOVCJr7n/qBCt53sOzA04zBeXoqnudud3KMhlJ1SH6Y42SYWzptiazGlL7DFOLTroSA+a5w/aftS88OnGlrHhm4Vl+VkMNaxt0cwts3Ne65b5FhhSWT3Mc4A='; | |
} | |
function () public payable {} | |
function __callback(bytes32 myid, string result) public { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
newYoutubeViewsCount(strConcat(bytes32ToString(myid), " query result: ", result)); | |
clients[myid].__oracle_callback(result); | |
} | |
function update(string videoId) public payable returns (bytes32) { | |
string memory url = strConcat('json(http://5.45.80.18/youtube_api_stats_intermediary.php?id=', videoId, ').items.0.statistics.viewCount'); | |
string memory post = apiKeyEncrypted; | |
bytes32 oqid = oraclize_query('URL', url, post); | |
newOraclizeQuery(strConcat("Oraclize query ", bytes32ToString(oqid), " was sent, standing by for the answer...")); | |
clients[oqid] = OracleDataReceiver(msg.sender); | |
return oqid; | |
} | |
// From https://ethereum.stackexchange.com/questions/2519/how-to-convert-a-bytes32-to-string | |
function bytes32ToString(bytes32 x) pure returns (string) { | |
bytes memory bytesString = new bytes(32); | |
uint charCount = 0; | |
for (uint j = 0; j < 32; j++) { | |
byte char = byte(bytes32(uint(x) * 2 ** (8 * j))); | |
if (char != 0) { | |
bytesString[charCount] = char; | |
charCount++; | |
} | |
} | |
bytes memory bytesStringTrimmed = new bytes(charCount); | |
for (j = 0; j < charCount; j++) { | |
bytesStringTrimmed[j] = bytesString[j]; | |
} | |
return string(bytesStringTrimmed); | |
} | |
} | |
//url = strConcat('json(https://www.googleapis.com/youtube/v3/videos?part=statistics&id=', videoId, '&key=', apiKey, ').items.0.statistics.likeCount'); | |
contract YoutubeOraclesTester { | |
uint public views; | |
//uint public likes; | |
YoutubeViewsOracle yvOracle; | |
//YoutubeLikesOracle ylOracle; | |
function YoutubeOraclesTester() public { | |
yvOracle = new YoutubeViewsOracle(); // Also may be passed as parameter | |
//ylOracle = new YoutubeLikesOracle(); | |
} | |
function updateYoutubeStats(string videoId) public { | |
yvOracle.update(videoId); | |
//ylOracle.update(videoId); | |
} | |
function __oracle_callback(string result) public { | |
if (msg.sender == address(yvOracle)) { | |
views = parseInt(result); | |
//} else if (msg.sender == address(ylOracle)) { | |
// likes = parseInt(result); | |
} else { | |
throw; | |
} | |
} | |
// From OraclizeAPI | |
function parseInt(string _a) internal returns (uint) { | |
return parseInt(_a, 0); | |
} | |
// From OraclizeAPI | |
function parseInt(string _a, uint _b) internal returns (uint) { | |
bytes memory bresult = bytes(_a); | |
uint mint = 0; | |
bool decimals = false; | |
for (uint i=0; i<bresult.length; i++){ | |
if ((bresult[i] >= 48)&&(bresult[i] <= 57)){ | |
if (decimals){ | |
if (_b == 0) break; | |
else _b--; | |
} | |
mint *= 10; | |
mint += uint(bresult[i]) - 48; | |
} else if (bresult[i] == 46) decimals = true; | |
} | |
if (_b > 0) mint *= 10**_b; | |
return mint; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment