Last active
May 16, 2018 15:29
-
-
Save Gim6626/e1d77dc846949946986d03a0e4c0a199 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 apiKey; | |
mapping(bytes32 => OracleDataReceiver) internal clients; | |
event newOraclizeQuery(string description); | |
event newYoutubeViewsCount(string views); | |
function YoutubeViewsOracle() payable public { | |
apiKey = 'API_KEY'; // TODO: Get from constructor's parameter and encrypt | |
} | |
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(https://www.googleapis.com/youtube/v3/videos?part=statistics&id=', videoId, '&key=', apiKey, ').items.0.statistics.viewCount'); | |
bytes32 oqid = oraclize_query('URL', url); | |
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