Skip to content

Create a module

Lasse Støjier Pedersen edited this page Feb 3, 2020 · 2 revisions

1. First what you need, the bots to do.

In this example we will create a module for changing the profile image of the each account. The page 'http://steamcommunity.com/actions/GameAvatars/' have a collection off image from game's that the locked in accout have.

2. What actions need to be none

  1. Enter page 'http://steamcommunity.com/actions/GameAvatars/'
  2. Click a image
  3. Submit profile image.

3. What happens behind the scene

  1. Enter 'http://steamcommunity.com/actions/GameAvatars/' get the html off the page.
  2. each image is just a normal link to a other page
  3. the submit is a formular, that send a Post request, to the games url like 'https://steamcommunity.com/games/TF2/selectAvatar', this data 'selectedAvatar, sessionid'.

4. What do we need

  1. Load 'http://steamcommunity.com/actions/GameAvatars/', and get html content
  2. get a url for a avatar.
  3. load that url
  4. get form data. 'action' and input name 'selectedAvatar'
  5. send post to the 'action' from 4. and 'selectedAvatar' from 4. and 'sessionid' that the module give you

5. the code

  1. add default data to module
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
	//for next step
};
  1. now we need to load the first page:
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
	var url = "http://steamcommunity.com/actions/GameAvatars/";
	RequestCommunity.get({uri: url}, function(error, response, body) {
		//for next step
	})
};

Here the body tag, have the html of the hole page. 3. we need at way to easy read the page, and find what we need. We do that using npm module 'cheerio' that is like Jquery. That way we can use Jquery to finde a random image, and get the url. each image have class 'oggAvatar' that way we just need to find a random one, of the tags.

var cheerio = require('cheerio');
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
    var url = "http://steamcommunity.com/actions/GameAvatars/";
    RequestCommunity.get({uri: url}, function(error, response, body) {
        var $ = cheerio.load(body);
        var avatarIndex = Math.floor(Math.random() * ($(".oggAvatar").length - 1));
        var gameAvatarUrl = $(".oggAvatar").eq(avatarIndex).find("a").attr("href");
        //for next step
    });
}
  1. now we just need to load that page, and get the content
var cheerio = require('cheerio');
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
    var url = "http://steamcommunity.com/actions/GameAvatars/";
    RequestCommunity.get({uri: url}, function(error, response, body) {
        var $ = cheerio.load(body);
        var avatarIndex = Math.floor(Math.random() * ($(".oggAvatar").length - 1));
        var gameAvatarUrl = $(".oggAvatar").eq(avatarIndex).find("a").attr("href");
        RequestCommunity.get({uri: gameAvatarUrl}, function(formError, formResponse, formBody) {
		var $form = cheerio.load(formBody);
		//for next step
        });
    });
}

here we just do as before, use RequestCommunity.get, to get page html. and add it into a jquery $form 5. now we just need to find the form in the html content, and get the action url, and the selectedAvatar input value

var cheerio = require('cheerio');
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
    var url = "http://steamcommunity.com/actions/GameAvatars/";
    RequestCommunity.get({uri: url}, function(error, response, body) {
        var $ = cheerio.load(body);
        var avatarIndex = Math.floor(Math.random() * ($(".oggAvatar").length - 1));
        var gameAvatarUrl = $(".oggAvatar").eq(avatarIndex).find("a").attr("href");
        RequestCommunity.get({uri: gameAvatarUrl}, function(formError, formResponse, formBody) {
            var $form = cheerio.load(formBody);
            var formHtml = $form("#avatarForm"); // find an id named avatarForm
            var formUrl = formHtml.attr("action"); // get the value of the attribute 'action'
            var selectedAvatar = formHtml.find("[name='selectedAvatar']").attr("value"); // find a element that have a attribute called 'name' and the value is selectedAvatar. and the that elements other attribute called 'value'
            //for next step
            
        });
    });
}

what we can see is the form have a id 'avatarForm'. when just use jquery to finde the id. and get the action url. 6. now we have all the info we need, we have the url, selectedAvatar and sessionId. we just have to send the post

var cheerio = require('cheerio');
module.exports = function(steamClient, RequestCommunity, RequestStore, SessionID, options, callback){
    var url = "http://steamcommunity.com/actions/GameAvatars/";
    RequestCommunity.get({uri: url}, function(error, response, body) {
        var $ = cheerio.load(body);
        var avatarIndex = Math.floor(Math.random() * ($(".oggAvatar").length - 1));
        var gameAvatarUrl = $(".oggAvatar").eq(avatarIndex).find("a").attr("href");
        RequestCommunity.get({uri: gameAvatarUrl}, function(formError, formResponse, formBody) {
            var $form = cheerio.load(formBody);
            var formHtml = $form("#avatarForm");
            var formUrl = formHtml.attr("action");
            var selectedAvatar = formHtml.find("[name='selectedAvatar']").attr("value");
            RequestCommunity.post({
                url: formUrl, // post url
                form:{ // data to send whit the post
                    selectedAvatar: selectedAvatar, // the new avartar
                    sessionid: SessionID // the account sessionID
                }
            }, function (postErr, postHttpResponse, postBody) {
                callback(); // callback when done! to ensure the flow go to next module or account
            });
        });
    });
}
Clone this wiki locally