Class: Kansou::AppStoreReview

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

Instance Method Summary collapse

Constructor Details

#initialize(app_id) ⇒ AppStoreReview

Returns a new instance of AppStoreReview.



6
7
8
9
10
# File 'lib/kansou/app_store_review.rb', line 6

def initialize(app_id)
  return nil unless app_id

  @app_id = app_id
end

Instance Method Details

#download(page = 0) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/kansou/app_store_review.rb', line 21

def download(page=0)
  base_url = "https://itunes.apple.com"
  user_agent = "iTunes/9.2 (Windows; Microsoft Windows 7 "\
                            + "Home Premium Edition (Build 7600)) AppleWebKit/533.16"

  url = base_url + "/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id="\
    + @app_id.to_s + "&pageNumber="+page.to_s+"&sortOrdering=4&type=Purple+Software"
  xml = open(url, 'User-Agent' => user_agent, 'X-Apple-Store-Front' => '143462-1').read
  return xml
end

#fetch(page_amount = 1) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/kansou/app_store_review.rb', line 11

def fetch(page_amount=1)
  reviews = []
  max_page = page_amount - 1
  (0..max_page).each do |page|
    xml = download(page)
    reviews.concat(parse(xml))
  end
  return reviews
end

#get_version(text) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/kansou/app_store_review.rb', line 99

def get_version(text)
  version = nil
  if /Version([\d\.]+)/ =~ text
    version = $1
  end
  return version
end

#parse(xml) ⇒ Object



32
33
34
35
36
37
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
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
94
95
96
97
# File 'lib/kansou/app_store_review.rb', line 32

def parse(xml)
  items = []
  users = []
  versions = []
  dates = []
  i = 0
  document = Oga.parse_xml(xml)

  expression = 'TextView[topInset="0"][styleSet="basic13"][squishiness="1"][leftInset="0"][truncation="right"][textJust="left"][maxLines="1"]'
  document.css(expression).each do |elm|
    if elm.text =~ /by/
      tmp_array = elm.text.gsub(" ", "").split("\n")
      info = []
      tmp_array.each do |v|
        if v!=""&&v!="-"&&v!="by"
          info.push v
        end
      end
      users.push info[0]

      if info[1]
        versions.push(get_version(info[1]))
      else
        versions.push("")
      end

      if info[2]
        dates.push(info[2])
      else
        dates.push("")
      end
    end
  end

  titles = []
  document.css('TextView[styleSet="basic13"][textJust="left"][maxLines="1"]').each do |elm|
    elm.css('b').each do |e|
      titles.push e.text
    end
  end

  stars = []
  document.css('HBoxView[topInset="1"]').each do |elm|
    stars.push elm.attribute("alt").value.gsub(/ stars*/,"")
  end

  bodies = []
  document.css('TextView[styleSet="normal11"]').each do |elm|
    bodies.push(elm.text.gsub("\n", "<br />"))
  end

  count = stars.size - 1
  (0..count).each do |key|
    item = {
      :star => stars[key],
      :user => users[key],
      :date => dates[key],
      :title => titles[key],
      :body => bodies[key],
      :version => versions[key],
      :app_id => @app_id
    }
    items.push(item)
  end
  return items
end