WIP Domain listing app with automatic status checking. Buitl with AngularJS.

addPost.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. /**
  3. * @ngdoc function
  4. * @name domainManagerApp.controller:MainCtrl
  5. * @description
  6. * # MainCtrl
  7. * Controller of the domainManagerApp
  8. */
  9. angular.module('domainManagerApp.addPost', ['firebase', 'domainManagerApp.userData'])
  10. .controller('AddPostController', ['$scope', 'UserData', '$firebase', '$http', function ($scope, UserData, $firebase, $http) {
  11. $scope.AddDomain = function() {
  12. console.log('Creating domain ' + $scope.domain.name);
  13. $scope.whoisLookup();
  14. }
  15. $scope.whoisLookup = function() {
  16. // Whois domain lookup
  17. console.log("Whois lookup...");
  18. var whoisUrl = 'https://j1x-whois.herokuapp.com/lookup.json?url=' + $scope.domain.name;
  19. $http({
  20. method: 'GET',
  21. url: whoisUrl
  22. }).success(function(data) {
  23. console.log("Whois lookup successfull");
  24. $scope.mapData(data);
  25. $scope.saveToFirebase();
  26. }).error(function(data, status, headers, config) {
  27. console.log("Whois lookup error");
  28. });
  29. }
  30. $scope.saveToFirebase = function() {
  31. // Save to Firebase
  32. var firebaseObj = new Firebase("https://j1x-cpanel.firebaseio.com/");
  33. var domainsRef = firebaseObj.child("domains");
  34. domainsRef.push({
  35. name: $scope.domain.name,
  36. userId: UserData.getUser(),
  37. registrar: $scope.domain.registrar,
  38. registration_date: $scope.domain.registration_date,
  39. expiration_date: $scope.domain.expiration_date,
  40. owner: $scope.domain.owner
  41. }, function(error) {
  42. if (error) {
  43. console.log('Error - Domain not created');
  44. } else {
  45. console.log('Domain created');
  46. }
  47. });
  48. }
  49. $scope.parseWhoisData = function(data) {
  50. var new_data = [];
  51. console.log(data);
  52. for (var i = 0; i < data.length; i++) {
  53. var keys = {};
  54. if (data[i] != ""){
  55. keys = data[i].split(": ")
  56. console.log(data[i]);
  57. console.log(keys);
  58. if(keys.length >= 2){
  59. var hash = new Array();
  60. hash[keys[0]] = keys[1];
  61. hash[0].replace(/\s+/g,"_").replace(/\.+/g,"_").replace(/\#+/g,"_").replace(/\$+/g,"_").replace(/\//g,"_").replace(/\[+/g,"_").replace(/\[+/g,"_");
  62. console.log(hash);
  63. new_data.push(hash);
  64. }
  65. }
  66. }
  67. console.log(new_data);
  68. return new_data;
  69. }
  70. // Map data from api.who.pm
  71. $scope.mapData = function(data) {
  72. console.log(data);
  73. // Data: Registrar
  74. if(data.registrar != undefined){
  75. $scope.domain.registrar = data.registrar;
  76. } else {
  77. $scope.domain.registrar = "";
  78. }
  79. // Data: Creation Date
  80. if(data.created_on != undefined) {
  81. $scope.domain.registration_date = data.created_on;
  82. } else {
  83. $scope.domain.registration_date = "";
  84. }
  85. // Data: Expiration Date
  86. if(data.expires_on != undefined) {
  87. $scope.domain.expiration_date = data.expires_on;
  88. } else {
  89. $scope.domain.expiration_date = "";
  90. }
  91. // Data Owner
  92. if(data.owner != undefined) {
  93. $scope.domain.owner = data.owner;
  94. } else {
  95. $scope.domain.owner = "";
  96. }
  97. }
  98. }]);