Simple Steem Random Comment Picker

in #utopian-io5 years ago


cover.png

SteemRandomPicker is a simple random comment picker which utilizes the Fisher-Yates shuffle algorithm to pick a random Steem comment. Its intended use could be in the selection of contest winner. User can filter based on follow, resteem, and reputation of participants.

Repository

https://github.com/CodeBull/SteemRandomPicker

Web

https://picker.steemify.me/

How to Use

  • Enter steem post's URL in the textbox. URL from any steem interface would work as long as it is in author/permalink format.
  • Select appropriate filters for the contest. Follow and resteem is not required by default. If required select required to participate, if follow and resteem should count as another entry select count as +1 entry. If you want to filter based on reputation, enter your minimum required reputation score.
  • Click get a random winner button.
  • A randomly selected winner will appear below along with comment body and permalink to the comment.
  • If there is any problem with the steem or hivemind node, a user can change nodes using the Settings modal.


ezgif.com-video-to-gif.gif

How does it work

At the beginning when page loads it sets default steem and hivemind nodes. If a user has previously saved settings then it replaces those with the user settings.

// Setting up default nodes
let steemdNode = 'https://anyx.io';
let hiveNode = 'https://tower.hive.oracle-d.com/api/v1';

if (localStorage.getItem('steemd')) steemdNode = localStorage.getItem('steemd');
if (localStorage.getItem('hive')) hiveNode = localStorage.getItem('hive');

Upon submission of the form, it will try to extract author and permalink from the steem post URL and fetch first level comments from the blockchain.

const url = $('#url').val();
const follow = $('input[name="follow"]').val();
const resteem = $('input[name="resteem"]').val();
const rep = $('input[name="min_rep"]').val();

const { author, permlink } = getAuthorAndPermlink(url);
// Getting all comments for the content
const comments = await client.database.call('get_content_replies', [author, permlink]);

Then it loops through all the comments and picks only required bits from the comment objects. It also calculates the reputation of every comment authors.

let users = comments
    .map(c => ({
      author: c.author,
      permlink: c.permlink,
      body: c.body,
      rep: calcReputation(c.author_reputation),
    }))
    .filter(u => u.rep >= rep);

After that, it checks if resteem or follow filters is set to required or not. If not required it ignores else it queries Hivemind RPC to get resteem and follow data for main post and its author.

if (resteem !== 'ignore') {
    const resteemQuery = await window.axios.get(`${hiveNode}/post_cache/${author}/${permlink}/reblogs/`);

    const resteems = resteemQuery.body.map(r => r.author);

    const resteemers = users.filter(u => resteems.includes(u.author));

    if (resteem === 'count') users.push(...resteemers);
    if (resteem === 'required') users = resteemers;
}

if (follow !== 'ignore') {
    const followersQuery = await window.axios.get(`${hiveNode}/accounts/${author}/followers/`);

    const followers = users.filter(u => followersQuery.body.followers.includes(u.author));

    if (follow === 'count') users.push(...followers);
    if (follow === 'required') users = followers;
  }

If follow and/or resteem is set to count as another entry, it pushes every qualified participant in the array which makes them appear twice and increase chances of their winning.

After preparing the array with all qualified participants, it shuffles the array and selects the first element of the array as the winner.

// Suffling filtered participants and selecting one
const winner = shuffleArray(users)[0];

Then it populates the HTML template, hides loading screen and shows the winner to the user.

let winnerText = resultText;

winnerText = winnerText.replace(/\{USERNAME\}/g, winner.author);
winnerText = winnerText.replace(/\{REP\}/g, winner.rep);
winnerText = winnerText.replace(/\{BODY\}/g, winner.body);
winnerText = winnerText.replace(/\{PERMLINK\}/g, winner.permlink);

$('#loading').hide();

$('.winner').show().html(winnerText);

Technology

Contributing

Feel free to fork the repository and make changes. If you find any issues please create a GitHub issue or let me know in the comments below.

GitHub

https://github.com/CodeBull/

Sort:  

Good to see tower API used by more applications. :)

Random comment picker has a simple and nice UI and works very well.

Is there a specific reason for using @ausbitbank's tower node instead of mine? (Not offended because of that, just wanna know if it has any problems or not.)


Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Chat with us on Discord.

[utopian-moderator]

Hi, thank you kindly for the review.

No there is no problem with the nodes. I was just testing with different nodes and check if I change settings it they change and persist. Then pushed the code like that. :P

In the GIF I used you node. 😉

Thank you so much for making Tower and running the Hivemind node.

Thank you for your review, @emrebeyler! Keep up the good work!

This post has been revived by steem-forever and will get extra rewards. This happens when a post is upvoted on steem-bounty.com after the 7 day post life.

Users can simple upvote via steem-bounty.com continously, so posts can live and earn rewards forever.
Authors can share their steem-bounty.com links and get upvoted forever.

We hope this will allow everyone to earn more meaningful rewards over longer timeframes than before.

This post has been revived by steem-forever and will get extra rewards. This happens when a post is upvoted on steem-bounty.com after the 7 day post life.

Users can simple upvote via steem-bounty.com continously, so posts can live and earn rewards forever.
Authors can share their steem-bounty.com links and get upvoted forever.

We hope this will allow everyone to earn more meaningful rewards over longer timeframes than before.

Hi @reazuliqbal!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @reazuliqbal!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Hi @reazuliqbal, your post has been upvoted by @bdcommunity and the trail!

If you want to support us, please consider following our curation trail, setting us as your witness proxy, or delegating STEEM POWER to us.

20 SP50 SP100 SP200 SP300 SP500 SP1000 SP

JOIN US ON

A very interesting post @reazuliqbal, I will analyze it carefully, thank you for sharing, I follow you ...

This project is now listed on SteemProjects. It's also included in weekly update post!

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 64176.22
ETH 2624.19
USDT 1.00
SBD 2.78