Class: GenerateList
- Inherits:
-
Object
- Object
- GenerateList
- Defined in:
- lib/wpstools/plugins/list_generator/generate_list.rb
Overview
This tool generates a list to use for plugin and theme enumeration
Instance Attribute Summary (collapse)
-
- (Object) verbose
Returns the value of attribute verbose.
Instance Method Summary (collapse)
- - (Object) generate_full_list
- - (Object) generate_popular_list(pages)
-
- (Object) get_popular_items(pages)
Send a HTTP request to the WordPress most popular theme or plugin webpage parse the response for the names.
-
- (GenerateList) initialize(type, verbose)
constructor
type = themes | plugins.
-
- (Object) save(items)
Save the file.
- - (Object) set_file_name(type)
Constructor Details
- (GenerateList) initialize(type, verbose)
type = themes | plugins
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 9 def initialize(type, verbose) if type =~ /plugins/i @type = 'plugin' @svn_url = 'http://plugins.svn.wordpress.org/' @popular_url = 'http://wordpress.org/plugins/browse/popular/' @popular_regex = %r{<h3><a href="http://wordpress.org/plugins/([^/]+)/">.+</a></h3>}i elsif type =~ /themes/i @type = 'theme' @svn_url = 'http://themes.svn.wordpress.org/' @popular_url = 'http://wordpress.org/themes/browse/popular/' @popular_regex = %r{<h3><a href="http://wordpress.org/themes/([^/]+)">.+</a></h3>}i else raise "Type #{type} not defined" end @verbose = verbose @browser = Browser.instance(request_timeout: 20000, connect_timeout: 20000, max_threads: 1, cache_ttl: 0) end |
Instance Attribute Details
- (Object) verbose
Returns the value of attribute verbose
6 7 8 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 6 def verbose @verbose end |
Instance Method Details
- (Object) generate_full_list
52 53 54 55 56 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 52 def generate_full_list set_file_name(:full) items = SvnParser.new(@svn_url).parse save items end |
- (Object) generate_popular_list(pages)
58 59 60 61 62 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 58 def generate_popular_list(pages) set_file_name(:popular) items = get_popular_items(pages) save items end |
- (Object) get_popular_items(pages)
Send a HTTP request to the WordPress most popular theme or plugin webpage parse the response for the names.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 66 def get_popular_items(pages) found_items = [] page_count = 1 (1...(pages.to_i + 1)).each do |page| # First page has another URL url = (page == 1) ? @popular_url : @popular_url + 'page/' + page.to_s + '/' puts "[+] Parsing page #{page_count}" if @verbose code = 0 while code != 200 puts red("[!] Retrying request for page #{page} (Code: #{code})") unless code == 0 request = @browser.forge_request(url) response = request.run code = response.code sleep(5) unless code == 200 end page_count += 1 found = 0 response.body.scan(@popular_regex).each do |item| found_items << item[0] found = found + 1 end puts "[+] Found #{found} items on page #{page}" if @verbose end found_items.sort! found_items.uniq end |
- (Object) save(items)
Save the file
96 97 98 99 100 101 102 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 96 def save(items) items.sort! items.uniq! puts "[*] We have parsed #{items.length} #{@type}s" File.open(@file_name, 'w') { |f| f.puts(items) } puts "New #@file_name file created" end |
- (Object) set_file_name(type)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/wpstools/plugins/list_generator/generate_list.rb', line 27 def set_file_name(type) case @type when 'plugin' case type when :full @file_name = PLUGINS_FULL_FILE when :popular @file_name = PLUGINS_FILE else raise 'Unknown type' end when 'theme' case type when :full @file_name = THEMES_FULL_FILE when :popular @file_name = THEMES_FILE else raise 'Unknown type' end else raise "Unknown type #@type" end end |