PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/share/spice/whois/whois.js

http://github.com/duckduckgo/zeroclickinfo-spice
JavaScript | 198 lines | 142 code | 26 blank | 30 comment | 32 complexity | fb4410980f2a73cdc119c359e043a773 MD5 | raw file
Possible License(s): Apache-2.0
  1. (function (env) {
  2. "use strict";
  3. env.ddg_spice_whois = function(api_result){
  4. if (!api_result || api_result.error || !api_result.WhoisRecord) {
  5. return Spice.failed('whois');
  6. }
  7. DDG.require('moment.js', function(){
  8. // get the search query
  9. var script = $('[src*="/js/spice/whois/"]')[0],
  10. source = $(script).attr("src"),
  11. query = source.replace('/js/spice/whois/','')
  12. // all the data is stored in WhoisRecord
  13. api_result = api_result.WhoisRecord;
  14. // fail if the domain name the api returns does not match the searched domain
  15. if(api_result.domainName != query) {
  16. return Spice.failed('whois');
  17. }
  18. // decide which template to show show_available or show_whois
  19. (is_domain_available(api_result)) ? show_available(api_result) : show_whois(api_result);
  20. });
  21. }
  22. // show message saying that the domain is available.
  23. function show_available(api_result) {
  24. var shared_spice_data = get_shared_spice_data(api_result),
  25. templateData = {
  26. 'domainRegistrars': {
  27. "Domainr": "https://domainr.com/",
  28. "NameCheap": "https://www.namecheap.com/domains/registration/results.aspx?domain=",
  29. "101domain": "https://www.101domain.com/domain-availability-search.htm?q="
  30. },
  31. 'domainName': api_result.domainName
  32. }
  33. shared_spice_data.data = templateData;
  34. shared_spice_data.templates.options.content = Spice.whois.available;
  35. Spice.add(shared_spice_data);
  36. };
  37. // show whois info for the domain using the 'record' template.
  38. function show_whois(api_result) {
  39. var shared_spice_data = get_shared_spice_data(api_result),
  40. nameServers,
  41. nsObj,
  42. normalized;
  43. // date formatting for moment.js
  44. // normalized dates require dateUTCParse custom format
  45. var dateOutputFormat = "MMM DD, YYYY",
  46. dateUTCParse = "YYYY-MM-DD HH:mm:ss Z";
  47. // set objects if they don't exist to prevent TypeError's with
  48. // undefined variables being tested as if they were objects.
  49. api_result.registryData = api_result.registryData || {};
  50. // store the domain's various contacts in an array.
  51. // we'll iterate through this array in order, using
  52. // info from the first contact that contains the field we want.
  53. var contacts = [
  54. api_result.registrant,
  55. api_result.administrativeContact,
  56. api_result.technicalContact,
  57. api_result.registryData.registrant,
  58. api_result.registryData.technicalContact,
  59. api_result.registryData.administrativeContact
  60. ];
  61. // find the nameservers
  62. if(api_result.nameServers) {
  63. nsObj = api_result.nameServers;
  64. } else if(api_result.registryData.nameServers) {
  65. nsObj = api_result.registryData.nameServers;
  66. }
  67. // check we have 2 nameserver hostnames
  68. if(nsObj && nsObj.hostNames) {
  69. if(nsObj.hostNames.length >= 2) {
  70. var nameServers = [nsObj.hostNames[0].toLowerCase(),nsObj.hostNames[1].toLowerCase()].join(' ');
  71. }
  72. }
  73. // find updatedDate and expiresDate in registryData
  74. if(!api_result.updatedDate && !api_result.expiresDate) {
  75. if(api_result.registryData.updatedDateNormalized && api_result.registryData.expiresDateNormalized) {
  76. api_result.updatedDate = api_result.registryData.updatedDateNormalized;
  77. api_result.expiresDate = api_result.registryData.expiresDateNormalized;
  78. }
  79. }
  80. // organize the data
  81. normalized = {
  82. 'title': api_result.domainName,
  83. 'Registered to': get_first_by_key(contacts, 'name'),
  84. 'Email': get_first_by_key(contacts, 'email'),
  85. 'Last Updated': prettifyTimestamp(api_result.updatedDate),
  86. 'Expires On': prettifyTimestamp(api_result.expiresDate),
  87. 'Registrar': api_result.registrarName,
  88. 'Name Servers': nameServers
  89. };
  90. // return nothing if we're missing all key whois data
  91. if(!normalized['Registered to']
  92. && !normalized['Email']
  93. && !normalized['Last Updated']
  94. && !normalized['Expires On']
  95. && !normalized['Name Servers']) {
  96. return;
  97. }
  98. // add the attributes specific to this template
  99. shared_spice_data.data = {
  100. 'record_data': normalized,
  101. 'record_keys': ['Registered to', 'Email', 'Last Updated', 'Expires On', 'Registrar', 'Name Servers']
  102. };
  103. shared_spice_data.templates.options.content = 'record';
  104. shared_spice_data.templates.options.keySpacing = true;
  105. Spice.add(shared_spice_data);
  106. };
  107. //Returns whether the domain is registered to someone, based on the API result.
  108. function is_domain_available(api_result) {
  109. return api_result.dataError && api_result.dataError === 'MISSING_WHOIS_DATA';
  110. }
  111. // Searches an array of objects for the first value
  112. // at the specified key.
  113. function get_first_by_key(arr, key) {
  114. var arrLength, i, obj;
  115. if(!arr || (arrLength = arr.length) === 0) {
  116. return;
  117. }
  118. for(i = 0; i < arrLength; ++i) {
  119. obj = arr[i];
  120. if(obj && obj[key]) {
  121. return obj[key];
  122. }
  123. }
  124. return;
  125. }
  126. //Converts timestamp into local time using moment.js
  127. function prettifyTimestamp(timestamp) {
  128. if(!timestamp) { return; }
  129. // dateDisplayFormat = Nov 21, 2022
  130. // normalizedFormat matches = 0000-00-00 00:00:00 TZ
  131. var dateDisplayFormat = "MMM DD, YYYY",
  132. normalizedFormat = "YYYY-MM-DD HH:mm:ss Z",
  133. autoDate = moment(timestamp);
  134. // some dates require custom date formatting
  135. // check if first date is vaild
  136. // if not format date using normalizedFormat
  137. // if no vaild date is found return
  138. if(autoDate.isValid()) {
  139. return autoDate.format(dateDisplayFormat);
  140. } else {
  141. var customDate = moment(timestamp, normalizedFormat);
  142. if(customDate.isValid()) {
  143. return customDate.format(dateDisplayFormat);
  144. } else {
  145. return;
  146. }
  147. }
  148. }
  149. // Data that's shared between the two Spice.add calls.
  150. function get_shared_spice_data(api_result) {
  151. return {
  152. id: 'whois',
  153. name: 'Whois',
  154. meta: {
  155. sourceName: 'Whois API',
  156. sourceUrl: 'http://whois.whoisxmlapi.com/'
  157. + api_result.domainName
  158. },
  159. normalize: function(item) {
  160. return {
  161. title: api_result.domainName
  162. };
  163. },
  164. templates: {
  165. group: 'list',
  166. options:{
  167. moreAt: true
  168. }
  169. }
  170. };
  171. };
  172. }(this));