ruby fake id generator API


SUBMITTED BY: Guest

DATE: Sept. 28, 2014, 8:01 p.m.

FORMAT: Text only

SIZE: 3.5 kB

HITS: 973

  1. #!/usr/bin/env ruby
  2. # encoding: UTF-8
  3. require 'selenium/webdriver'
  4. require 'watir-webdriver'
  5. require 'nokogiri'
  6. module FakeId
  7. class Generator
  8. START_URL = "http://www.fakenamegenerator.com/"
  9. =begin rdoc
  10. In order to create a valid instance pass keywords and localisation to scrap.
  11. =end
  12. def initialize
  13. make_agent
  14. end
  15. def finalize
  16. kill_agent
  17. end
  18. =begin rdoc
  19. Connect to the main page and set elements used to perform search.
  20. =end
  21. def gostart
  22. @b.goto START_URL
  23. end
  24. def process
  25. @b.select_list(:id, "gen").select_value("female") # female
  26. @b.select_list(:id, "n").select_value("us") # fr
  27. @b.select_list(:id, "c").select_value("us") # fr
  28. @b.button(:id,"genbtn").click
  29. end
  30. =begin rdoc
  31. Parse each result page to format informations to extract.
  32. =end
  33. def get_infos_pages
  34. doc = Nokogiri::HTML.parse(@b.html)
  35. #doc.css('br').each{ |br| br.replace(" ") }
  36. name = doc.search('//html/body/div/div[2]/div/div/div/div[3]/div[2]/div[2]/div/div/h3').text
  37. return name
  38. #infos = doc.search('//div[@id="details"]').map do | row |
  39. # {
  40. #'name' => row.xpath('/div[2]/div[2]/div/div[1]/h3').text#,
  41. #'addr' => row.xpath('/div[2]/div[2]/div/div[1]/div').text,
  42. #'tel' => row.xpath('/div[2]/div[2]/div/div[2]/ul/li[2]/span').text,
  43. #'mail' => row.xpath('div[2]/div[2]/div/div[2]/ul/li[4]/span').text,
  44. #'active_mail' => row.xpath('/div[2]/div[2]/div/div[2]/ul/li[4]/div/a').text,
  45. #'login' => row.xpath('/div[2]/div[2]/div/div[2]/ul/li[6]').text,
  46. #'password' => row.xpath('/div[2]/div[2]/div/div[2]/ul/li[8]').text,
  47. #'mother_nm' => rowdoc.xpath('/div[2]/div[2]/div/div[2]/ul/li[10]').text,
  48. #'birthday' => row.xpath('/div[2]/div[2]/div/div[2]/ul/li[12]').text,
  49. #}
  50. #end
  51. #return infos
  52. end
  53. =begin rdoc
  54. Write formatted informations in output file.
  55. =end
  56. def write_infos(file, infos)
  57. infos.each do |bloc|
  58. bloc.each do |key,value|
  59. file.write value << ";" unless value.empty?
  60. end
  61. file.write "\n"
  62. end
  63. end
  64. def write_name(file, infos)
  65. file.write infos
  66. file.write "\n"
  67. end
  68. =begin rdoc
  69. Browse all results pages and extract wished informations.
  70. =end
  71. def scrape_all(file)
  72. begin
  73. infos = get_infos_pages
  74. write_name(file, infos)
  75. rescue Exception => e
  76. #puts e.message
  77. end
  78. end
  79. private
  80. def make_agent
  81. @b = Watir::Browser.new :phantomjs
  82. end
  83. def kill_agent
  84. @b.close
  85. end
  86. end
  87. end
  88. def main
  89. include FakeId
  90. id = Generator.new
  91. sentence = "id_ls.txt"
  92. fic_out =File.open(sentence,'a')
  93. id.gostart
  94. begin
  95. 150000.times do
  96. id.process
  97. id.scrape_all(fic_out)
  98. end
  99. rescue Exception => e
  100. puts e.message
  101. ensure
  102. fic_out.close
  103. id.finalize
  104. end
  105. end
  106. main

comments powered by Disqus