Last active
April 24, 2018 05:21
-
-
Save Gim6626/ff34b22b34d36d9ce89370867bfe4b21 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
pragma solidity ^0.4.0; | |
import "github.com/oraclize/ethereum-api/oraclizeAPI.sol"; | |
interface YoutubeViewsReceiver { | |
function updateYoutubeViewsInternal(uint videoViews); | |
} | |
interface YoutubeLikesReceiver { | |
function updateYoutubeLikesInternal(uint videoLikes); | |
} | |
contract YoutubeViewsOracle is usingOraclize { | |
uint public viewsCount; | |
string internal apiKey; | |
string internal url; | |
YoutubeViewsReceiver receiver; | |
event newOraclizeQuery(string description); | |
event newYoutubeViewsCount(uint views); | |
function YoutubeViewsOracle(YoutubeViewsReceiver receiverAddress) payable public { | |
apiKey = 'API_KEY'; // TODO: Get from constructor's parameter or somewhere else | |
receiver = receiverAddress; | |
} | |
function () public payable {} | |
function __callback(bytes32 myid, string result) public { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
viewsCount = parseInt(result); | |
newYoutubeViewsCount(viewsCount); | |
receiver.updateYoutubeViewsInternal(viewsCount); | |
} | |
function update(string videoId) public payable { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
url = strConcat('json(https://www.googleapis.com/youtube/v3/videos?part=statistics&id=', videoId, '&key=', apiKey, ').items.0.statistics.viewCount'); | |
oraclize_query('URL', url); | |
} | |
} | |
contract YoutubeLikesOracle is usingOraclize { | |
uint public likesCount; | |
string internal apiKey; | |
string internal url; | |
YoutubeLikesReceiver receiver; | |
event newOraclizeQuery(string description); | |
event newYoutubeLikesCount(uint likes); | |
function YoutubeLikesOracle(YoutubeLikesReceiver receiverAddress) payable public { | |
apiKey = 'API_KEY'; // TODO: Get from constructor's parameter or somewhere else | |
receiver = receiverAddress; | |
} | |
function () public payable {} | |
function __callback(bytes32 myid, string result) public { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
likesCount = parseInt(result); | |
newYoutubeLikesCount(likesCount); | |
receiver.updateYoutubeLikesInternal(likesCount); | |
} | |
function update(string videoId) public payable { | |
newOraclizeQuery("Oraclize query was sent, standing by for the answer.."); | |
url = strConcat('json(https://www.googleapis.com/youtube/v3/videos?part=statistics&id=', videoId, '&key=', apiKey, ').items.0.statistics.likeCount'); | |
oraclize_query('URL', url); | |
} | |
} | |
contract YoutubeOraclesTester { | |
uint public views; | |
uint public likes; | |
YoutubeViewsOracle yvOracle; | |
YoutubeLikesOracle ylOracle; | |
function YoutubeOraclesTester() public { | |
yvOracle = new YoutubeViewsOracle(YoutubeViewsReceiver(this)); | |
ylOracle = new YoutubeLikesOracle(YoutubeLikesReceiver(this)); | |
} | |
function updateYoutubeStats(string videoId) public { | |
yvOracle.update(videoId); | |
ylOracle.update(videoId); | |
} | |
function updateYoutubeViewsInternal(uint videoViews) public { | |
require(msg.sender == address(yvOracle)); | |
views = videoViews; | |
} | |
function updateYoutubeLikesInternal(uint videoLikes) public { | |
require(msg.sender == address(ylOracle)); | |
likes = videoLikes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment