Close
0%
0%

Hack-a-blog using the API

Turn project logs from a Hackaday.io project into a blog on your external website

Similar projects worth following
Since Blogger and WordPress totally suck, I thought it would be nice to eliminate the need for a blog platform and instead just pull in all of my project logs from Hackaday to serve as a blog. This is pretty straightforward using the Hackaday API. Here is an example:

Stickvise project logs: https://hackaday.io/project/3287/logs/sort/newest
Stickvise blog on external site: http://www.stickvise.com/blog/1

All you have to do is sign up for an API account (https://dev.hackaday.io/ ) and use the little bit of PHP that I describe in the project details. Once you have it in place, just post project logs on Hackaday.io as you normally would, they will be mirrored on your personal website as a blog.

create a PHP file for your blog called "blog.php" add the snippet below anywhere you want the html for your blog to be inserted. Keep in mind, the HTML will be totally un-styled so you may have to do a little CSS work to match your site's formatting and style.

<?php include("include/return_blog.php"); ?>

Next create a file called "return_blog.php" with the code below in it. Be sure to update the pertinent variables (read my comments in the code snippet). Put this file into a sub-folder called "include"

<?php
// Hackaday.io Blog Widget
// This code turns your project logs into a simple blog for your external website

// **** YOU MUST EDIT THE API AND PROJECT ID PARAMETERS BELOW TO REFLECT YOUR https://dev.hackaday.io/ API ACCOUNT ******

// enter your api key
$api_key = "XXXXXXXXXXXXXXXX";

// enter your project id, you can find it in the url of your project
$project_id = "XXXX";

// enter your blog url, for my blog it is http://www.stickvise.com/blog/
$blog_url = "http://xxxx";

// number of logs to display per page
$logs_per_page = 4;

// if $log_page is not set, default $log_page to 1 
$log_page = ($_GET["log_page"] ? $_GET["log_page"] : 1);

// import json data from API
// get project object
$project_object = json_decode(file_get_contents("https://api.hackaday.io/v1/projects/".$project_id."?api_key=".$api_key),true);
// get array of project log objects (starting at $log_page, quantity limited by $logs_per_page)
$log_objects_array = json_decode(file_get_contents("https://api.hackaday.io/v1/projects/".$project_id."/logs?api_key=".$api_key."&sortby=newest&page=".$log_page."&per_page=".$logs_per_page),true);

// store the project url in a variable, this is the base for the individual log URLs
$project_url = $project_object['url'];
// store the total number of logs for this project in a variable
$project_numlogs = $project_object['logs'];

// loop through the log_objects_array and spit out HTML
foreach($log_objects_array['logs'] as $log) {
  
  $log_title = $log['title'];
  $log_body = $log['body'];
  $log_id = $log['id'];
  $log_date = $log['created'];
  // get the main comment object for each log
  $log_comments = json_decode(file_get_contents("https://api.hackaday.io/v1/comments/logs/".$log_id."?api_key=".$api_key),true);
  
  // Print the log title and body
  echo '
     <h2><a href="'.$project_url.'/log/'.$log_id.'" target="_blank">'.$log_title.'</a><br/>
      <small>'.date('M-d Y', $log_date).'</small>
     </h2>
      <p>
          '.$log_body;

  // Print log comments (if there are any for this log)
  if ($log_comments['total']) {
    echo '<h3>COMMENTS:</h3>';  
    print_comments($log_comments['comments']); // magic function
  }

    
  // Print the remainder of HTML
  echo '
       </p>
  ';
} // end loop through logs

// Print bottom nav
  echo '
    <h2>
    ';
  if($log_page > 1) {
    echo '
    <a href="'.$blog_url.($log_page-1).'">&#60;&#60; Newer</a>&nbsp;&nbsp;&nbsp;
      ';
  } 
  if ($log_page+1 <= $project_numlogs/$logs_per_page) {
    echo '
    <a href="'.$blog_url.($log_page+1).'">Older &#62;&#62;</a>
      ';
  }
  echo '
    </h2>
    ';

// print_comments function
function print_comments($comments) {
  
  // loop through comments array
  echo '<ul>';
  foreach($comments as $comment_object1) {        
    // find and print first tier comments 
    if($comment_object1['reply_to'] == 0) {
      echo '
      <p><a href="'.$comment_object1['user']['url'].'" target="_blank">'.$comment_object1['user']['screen_name'].'</a><br />
      '.$comment_object1['comment'].'
      </p>';
      
      // loop through comments array again to find second tier comments
      echo '<ul>';
      foreach($comments as $comment_object2) {
        // find and print second tier comments replying to the above tier comment
        if($comment_object2['reply_to'] == $comment_object1['id']) {
          echo '
          <p><a href="'.$comment_object2['user']['url'].'" target="_blank">'.$comment_object2['user']['screen_name'].'</a><br />
          '.$comment_object2['comment'].'
          </p>';
          
          // loop through comments array third time to find third tier comments
          echo '<ul>';
          foreach($comments as $comment_object3) {
            // find and print...
Read more »

return_blog.php

put this in the /include folder

php - 4.35 kB - 07/19/2016 at 00:58

Download

.htaccess

add to your root, or open the file and add to your existing .htaccess

htaccess - 75.00 bytes - 07/19/2016 at 00:58

Download

blog.php

example blog website (way oversimplified, you must add your own styles)

php - 127.00 bytes - 07/19/2016 at 00:58

Download

  • Comments now displaying using brute force!

    Alex Rich07/15/2016 at 03:07 4 comments

    Some how, some way I figured out how to display the comments. It's a total butcher job of the code, a better programmer probably would have some elegant way to do this with recursion or something, instead I just used three for loops. But hey, it's working! Good example of comments displaying (even third tier level) is shown on this page:

    http://www.stickvise.com/blog/2

    Here is the new print_comments function I made to brute force the display of comments in the correct nested manner. I

    // print_comments function
    function print_comments($comments) {
      
      // loop through comments array
      echo '<ul>';
      foreach($comments as $comment_object1) {        
        // find and print first tier comments 
        if($comment_object1['reply_to'] == 0) {
          echo '
          <p><a href="'.$comment_object1['user']['url'].'" target="_blank">'.$comment_object1['user']['screen_name'].'</a><br />
          '.$comment_object1['comment'].'
          </p>';
          
          // loop through comments array again to find second tier comments
          echo '<ul>';
          foreach($comments as $comment_object2) {
            // find and print second tier comments replying to the above tier comment
            if($comment_object2['reply_to'] == $comment_object1['id']) {
              echo '
              <p><a href="'.$comment_object2['user']['url'].'" target="_blank">'.$comment_object2['user']['screen_name'].'</a><br />
              '.$comment_object2['comment'].'
              </p>';
              
              // loop through comments array third time to find third tier comments
              echo '<ul>';
              foreach($comments as $comment_object3) {
                // find and print third tier comments replying to the second tier comment
                if($comment_object3['reply_to'] == $comment_object2['id']) {
                  echo '
                  <p><a href="'.$comment_object3['user']['url'].'" target="_blank">'.$comment_object3['user']['screen_name'].'</a><br />
                  '.$comment_object3['comment'].'
                  </p>';
                }
              }
              echo '</ul>'; // end third tier comments
            }
          }
          echo '</ul>'; // end second tier comments
        }
      }
      echo '</ul>'; // end first tier comments
    } // end print_comments()

View project log

Enjoy this project?

Share

Discussions

jacksonville234567 wrote 03/10/2023 at 19:38 point

There has been a great deal of value to me in my involvement with the project. Would like to share it with the https://gofinanc.com/ team so they can also read it and implement something new.

  Are you sure? yes | no

debinalsa wrote 03/09/2022 at 15:28 point

This is pretty unique idea and you can check some information about the similar kind of different category welder reviews blog and learn about this welder product.

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates