Class: EFFScraper

Inherits:
Object
  • Object
show all
Defined in:
lib/effscraper.rb

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ EFFScraper

Returns a new instance of EFFScraper.



7
8
9
10
# File 'lib/effscraper.rb', line 7

def initialize(url)
  @url = url
  @casearray = Array.new
end

Instance Method Details

#scrapeCaseObject

Scrapes all documents in case



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/effscraper.rb', line 13

def scrapeCase
  html = Nokogiri::HTML(open(@url))

  # Get number of pages to scrape
  if html.css("li.pager-current")[0]
    count = html.css("li.pager-current")[0].text.split(" ")
    n = count[2].to_i
  else
    n = 1
  end

  # Go through pages and scrape them
  for i in 1..n
    if i > 1
      link = "https://eff.org" + html.css("li.pager-next")[0].css("a")[0]["href"]
      html = Nokogiri::HTML(open(link))
    end
    
    scrapePage(html)
  end
  
  JSON.pretty_generate(@casearray)
end

#scrapePage(html) ⇒ Object

Scrapes each page of documents



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/effscraper.rb', line 38

def scrapePage(html)
  items = html.css("div.view-content")[0]
  
  items.css("li").each do |l|
    dochash = Hash.new

    # Gets link to document and file
    l.css("a").each do |a|
      if a.text == "[PDF]"
        dochash[:url] = a["href"]
        `wget #{dochash[:url]}` 
        path = dochash[:url].split("/")
        dochash[:path] = path[path.length-1].chomp.strip
      end
    end

    # Get date and title                                                      
    dochash[:doc_date] = l.css("span.date-display-single").text
    dochash[:title] = l.css("a")[1].text

    # Extract metadata and text
    begin
      u = UploadConvert.new(dochash[:path])
       = u.extractMetadataPDF
      .each{|k, v| dochash[k] = v}
      dochash[:text] = u.detectPDFType
      @casearray.push(dochash)
    rescue
    end
  end
end