kinlane
3/14/2018 - 2:29 AM

Stream to Github

Stream to Github

<h1>Stream to Github</h1>
<p>This will stream news from the NewsAPI using Streamdata.io, then write results to the Github repository this project runs in.</p>
<p>It takes three sets of keys to be able to pull, proxy, and write to the repository.</p>
Streamdata Key: <input type="text" name="streamdataKey" id="streamdataKey" value=""><br />
News API Key: <input type="text" name="newsAPIKey" id="newsAPIKey" value=""><br />
Github Token: <input type="text" name="githubtoken" id="githubtoken" value=""><br />
<input type="button" name="run" value="run" onclick="runit();">

<script>

function processResults($results)
	{

	$result = JSON.stringify($results);

	// Grab the github oauth token
	var $github_token = document.getElementById("githubtoken").value

	// Set the user/org and the repo
	$github_owner = 'streaming-api-projects';
	$github_repo = 'stream-to-github';
	$file_name = 'news.json';

	$token = 'Authorization: token ' + $github_token;
	$headers = [$token,'User-Agent: Streaam-To-github'];

	$githuburl = 'https://api.github.com/repos/' + $github_owner + '/' + $github_repo + '/contents/' + $file_name;

	// Get the file, so we have the SHA
	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open( "GET", $githuburl, false ); // false for synchronous request
	xmlHttp.setRequestHeader("Authorization",'token ' + $github_token);
	xmlHttp.send( null );
	$file_response = JSON.parse(xmlHttp.responseText);

	// write the file to Github repo using github API
	$body = {};
	$body['message'] = "Write File";
	$body['content'] = btoa(unescape(encodeURIComponent($result)));
	$body['sha'] = $file_response['sha'];
	$body_json = JSON.stringify($body);

	var xmlHttp = new XMLHttpRequest();
	xmlHttp.open("PUT", $githuburl, false ); // false for synchronous request
	xmlHttp.setRequestHeader("Authorization",'token ' + $github_token);
	xmlHttp.setRequestHeader("Content-Type", "application/json");
	xmlHttp.send($body_json);

	console.log(xmlHttp.responseText);
	//$file_response = JSON.parse(xmlHttp.responseText);

	}

function runit()
	{

  var eventSource = null;
  var stocks = [];
  var checked = 1;

  function connect() {

		// Grab the streamdata key
		var $streamdataKey = document.getElementById("streamdataKey").value;
		console.log($streamdataKey);

		// grab the newsAPI key
		var $newsAPIKey = document.getElementById("newsAPIKey").value;
		console.log($newsAPIKey);

		// Set the URL
		var $api_url = 'http://newsapi.org/v2/top-headlines?country=us&apiKey=' + $newsAPIKey;
		console.log($api_url);

    // create the StreamdataEventSource Object
    eventSource = streamdataio.createEventSource($api_url, $streamdataKey);

    eventSource
     .onOpen(function() {
       console.log("streamdata Event Source connected.")
     })
     .onData(function(data) {
			 	// process results
	   		processResults(data);
     })
     .onPatch(function(patch) {
       	results = patch.events;
				// process results
				processResults(results);
     })
     .onError(function(error) {
       // displays the error message
       console.log(error.getMessage());
     });
    // open the data stream to the REST service through streamdata.io proxy
    eventSource.open();
  };

  function disconnect() {
    if (eventSource) {
      eventSource.close();
      eventSource = null;
    }
  };

  connect();

	}

</script>