Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var https = require('https');
  2. var events = require('events');
  3. var fs = require('fs');
  4. var PUBNUB = require('./pubnub/node.js/pubnub.js');
  5.  
  6. var HOST = "api.dubtrack.fm"
  7.  
  8. var request_cookie = "";
  9.  
  10. var room_id;
  11.  
  12. var userdata;
  13.  
  14. var _pubnub;
  15.  
  16. var room;
  17.  
  18.  
  19. function dump(object)
  20. {
  21.     console.log("my object: %o", object)
  22. }
  23.  
  24. // callback(response, data)
  25. function doRequest(callback, method, hostname, path, data)
  26. {
  27.     //var redirected_n = 0
  28.     var options = {
  29.         'hostname': hostname,
  30.         'path': path,
  31.         'method': method,
  32.     };
  33.     var req = https.request(options, function (response) {
  34.         var body = '';
  35.         response.on('data', function(d) {
  36.             body += d;
  37.         });
  38.         var _response = response;
  39.         /*if (response.headers.location && redirected_n < 10)
  40.         {
  41.             console.log("redirected to " + response.headers.location)
  42.             doRequest(callback, method, hostname, response.headers.location, data);
  43.             redirected_n++;
  44.             return;
  45.         }*/
  46.         response.on('end', function () { callback(_response, body); });
  47.     });
  48.     req.setHeader("cookie", request_cookie);
  49.     req.setHeader("content-type", "application/json");
  50.     if (req == 'POST')
  51.     {
  52.         req.write(data);
  53.     }
  54.     req.end();
  55. }
  56.  
  57. function arrayToCookie(array)
  58. {
  59.     var cookie = "";
  60.     for (key in array)
  61.     {
  62.         cookie = cookie + " " + key + "=" + array[key] + ";";
  63.     }
  64.     console.log(cookie);
  65.     return cookie
  66. }
  67.  
  68. function authCallback(response, data)
  69. {
  70.     if (response.statusCode == 200)
  71.     {
  72.         console.log("login ok" );
  73.         request_cookie =response.headers["set-cookie"][0];
  74.         doRequest(loginCallback, "GET", HOST, "/auth/session");
  75.         return;
  76.     }
  77.     console.log("login not ok");
  78.     process.exit(1);
  79. }
  80.  
  81. function sessionCallback(response, data)
  82. {
  83.    
  84.     /*var parsed = JSON.parse(data);
  85.     if (parsed.code == 200)
  86.     {
  87.         console.log(data);
  88.         return;
  89.     }*/
  90.     console.log(data);
  91.     doRequest(loginCallback, "GET", HOST, response.headers['location']);
  92. }
  93.  
  94. function loginCallback(response, data)
  95. {
  96.     var parsed = JSON.parse(data);
  97.     console.log("logged in");
  98.     if (parsed.code == 200)
  99.     {
  100.         userdata = parsed.data;
  101.         console.log(data);
  102.         doRequest(roomCallback, "GET", HOST, "/room/" + process.argv[4]);
  103.         return;
  104.     }
  105. }
  106.  
  107. function roomCallback(response, data)
  108. {
  109.     var parsed = JSON.parse(data);
  110.     if (parsed.code == 200)
  111.     {
  112.         console.log(parsed.data.name);
  113.         console.log("id: " + parsed.data._id);
  114.         console.log("current video: " + parsed.data.currentSong.name);
  115.         room = parsed.data
  116.         room_id = parsed.data._id;
  117.         joinPubNubChannel();
  118.         console.log("/chat/" + room_id);
  119.         doRequest(chatoptionsCallback, "GET", HOST, "/message/new");
  120.         return;
  121.     }
  122.     console.log("Couldn't get room info");
  123.     process.exit(1);
  124. }
  125.  
  126. function joinPubNubChannel()
  127. {
  128.     _pubnub = PUBNUB.init({
  129.         backfill: false,
  130.         restore: false,
  131.         subscribe_key: 'sub-c-2b40f72a-6b59-11e3-ab46-02ee2ddab7fe',
  132.         ssl: true,
  133.         uuid: userdata._id
  134.     });
  135.     _pubnub.subscribe({
  136.         'channel' : room.realTimeChannel,
  137.         'message' : function( message, env, channel ){ console.log(message);
  138.             if (message.message == "bot")
  139.             {
  140.                 send_msg("egazhpohazehgzhegphhgehpegzphegzhpgezhpgepeHPGHUGHZE");
  141.             }
  142.             if (message.message == "bot_updub")
  143.             {
  144.                 send_msg("egazhpohazehgzhegphhgehpegzphegzhpgezhpgepeHPGHUGHZE");
  145.             }
  146.         },
  147.         'connect' : function(){console.log("Connected");},
  148.         'disconnect' : function(){console.log("Disconnected");},
  149.         'reconnect' : function(){console.log("Reconnected");},
  150.         'error' : function(){console.log("Network Error");}
  151.     });
  152. }
  153.  
  154. function send_msg(msg)
  155. {
  156.     var msg = {
  157.         'user' : userdata,
  158.         'message' : msg,
  159.         'time' : Date.now(),
  160.         'realTimeChannel' : room.realTimeChannel,
  161.         'type' : "chat-message",
  162.         'chatid': null,
  163.         //'lastlogin': 0
  164.     };
  165.     console.log(JSON.stringify(msg))
  166.     doRequest(function(response, data) { console.log(data); }, "POST", HOST, "/chat/" + room_id, JSON.stringify(msg));
  167.     /*_pubnub.publish({
  168.     channel: room.realTimeChannel,        
  169.     message: 'Hello from the PubNub Javascript SDK!',
  170.     callback : function(m){console.log(m)}
  171.     });*/
  172. }
  173. function chatoptionsCallback(response, data)
  174. {
  175.     dump(response.headers);
  176.     console.log(data);
  177.     return;
  178. }
  179.  
  180. doRequest(authCallback, "POST", HOST, "/auth/dubtrack?username=" + process.argv[2] + "&password=" + process.argv[3], "");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement