Skip to content

Commit 4218b49

Browse files
author
Simon Funke
committed
Conflicts: Makefile db/seeds.rb
2 parents 160f20f + 3c8739c commit 4218b49

11 files changed

+70
-26
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ default:
22
rake db:drop
33
rake db:create
44
rake db:migrate
5-
rake load_mlab_data:all
65
rake db:seed
6+
rake load_mlab_data:all
7+
rake load_transparencyreport_data:all

app/models/content_removal_request.rb

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class ContentRemovalRequest < ActiveRecord::Base
22
belongs_to :content_removal_request_period
3+
belongs_to :product
4+
belongs_to :reason
35

4-
has_many :products
5-
has_many :reasons
6-
7-
attr_accessible :courtOrders, :executive, :items
6+
attr_accessible :court_orders, :executive, :items
87
end
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class ContentRemovalRequestPeriod < ActiveRecord::Base
2+
belongs_to :country
3+
24
has_many :content_removal_requests
3-
has_one :country
45

56
attr_accessible :period_start, :period_end, :percentage_complied
67
end

app/models/country.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Country < ActiveRecord::Base
22
belongs_to :continent
3-
belongs_to :content_removal_request_period
3+
4+
has_many :content_removal_request_periods
5+
has_many :content_removal_requests, :through => :content_removal_request_periods
46

57
validates_presence_of :code, :name
68
validates_uniqueness_of :code

app/models/product.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Product < ActiveRecord::Base
2-
belongs_to :content_removal_request
2+
has_many :content_removal_requests
33

44
attr_accessible :name
55
end

app/models/reason.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Reason < ActiveRecord::Base
2-
belongs_to :content_removal_request
2+
has_many :content_removal_requests
33

44
attr_accessible :name
55
end

db/migrate/20111109012514_create_content_removal_requests.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ def self.up
44
t.integer :content_removal_request_period_id
55
t.integer :product_id
66
t.integer :reason_id
7-
t.integer :courtOrders
7+
t.integer :court_orders
88
t.integer :executive
99
t.integer :items
1010
t.timestamps

db/schema.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
t.integer "content_removal_request_period_id"
5252
t.integer "product_id"
5353
t.integer "reason_id"
54-
t.integer "courtOrders"
54+
t.integer "court_orders"
5555
t.integer "executive"
5656
t.integer "items"
5757
t.datetime "created_at"

db/seeds.rb

+3-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,8 @@
1111

1212
h = Hash.from_xml(open('https://raw.github.com/ChokePointProject/cpp-web/master/tmp/countryInfo.xml'))['geonames']['country']
1313

14-
# 1st sweep: continents
1514
h.each do |c|
16-
Continent.find_or_create_by_code_and_name(c['continent'], c['continentName'])
17-
end
18-
19-
# 2nd sweep: countries
20-
h.each do |c|
21-
Country.create(
22-
:continent_id => Continent.find_by_code(c['continent']),
15+
n = Country.new(
2316
:code => c['countryCode'],
2417
:name => c['countryName'],
2518
:isoNumeric => c['isoNumeric'],
@@ -36,6 +29,7 @@
3629
:bBoxEast => c['bBoxEast'],
3730
:bBoxSouth => c['bBoxSouth']
3831
)
39-
32+
n.continent = Continent.find_or_create_by_code_and_name(c['continent'], c['continentName'])
33+
n.save!
4034
end
4135

lib/tasks/load_transparencyreport_data.rake

+51-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ namespace :load_transparencyreport_data do
22
require 'csv'
33
require 'open-uri'
44

5-
desc "Loading transparencyreport data"
5+
desc "Loading transparencyreport data: content removal request"
66
task :load_content_removal_requests => :environment do
7-
#csv_text = File.read('https://www.google.com/transparencyreport/governmentrequests/google-content-removal-requests-by-product-and-reason.csv')
7+
# Read in XML data
88
c = CSV.parse(open('https://www.google.com/transparencyreport/governmentrequests/google-content-removal-requests.csv'), :headers => true)
99
c.each do |r|
1010
period_end = Date.strptime(r[0], '%m/%d/%Y')
@@ -13,14 +13,61 @@ namespace :load_transparencyreport_data do
1313
:period_end => period_end,
1414
:percentage_complied => r[4]
1515
)
16-
#puts r[2], Country.find_by_code(r[2])
1716
e.country_id = Country.find_by_code(r[2]).id
1817
e.save!
1918
end
2019
end
2120

21+
desc "Loading transparencyreport data: content removal request by product"
22+
task :load_content_removal_requests_by_product => :environment do
23+
# Create reason 'undefined' (since this dataset doesn't contain reasons)
24+
undefined = Reason.find_or_create_by_name('Undefined')
25+
# Read in XML data
26+
c = CSV.parse(open('https://www.google.com/transparencyreport/governmentrequests/google-content-removal-requests-by-product.csv'), :headers => true)
27+
c.each do |r|
28+
# Only add entries prior from 2010-12-31
29+
if Date.strptime(r[0], '%m/%d/%Y') < Date.new(2010,12,31)
30+
e = ContentRemovalRequest.new(
31+
:court_orders => r[4],
32+
:executive => r[5],
33+
:items => r[6]
34+
)
35+
e.content_removal_request_period = ContentRemovalRequestPeriod.joins(:country).where('countries.code' => r[2]).find_by_period_end(Date.strptime(r[0], '%m/%d/%Y'))
36+
e.product = Product.find_or_create_by_name(r[3])
37+
e.reason = undefined
38+
e.save!
39+
end
40+
end
41+
end
42+
43+
desc "Loading transparencyreport data: content removal request by product and reason"
44+
task :load_content_removal_requests_by_product_and_reason => :environment do
45+
# Read in XML data
46+
c = CSV.parse(open('https://www.google.com/transparencyreport/governmentrequests/google-content-removal-requests-by-product-and-reason.csv'), :headers => true)
47+
c.each do |r|
48+
period = ContentRemovalRequestPeriod.joins(:country).where('countries.code' => r[2]).find_by_period_end(Date.strptime(r[0], '%m/%d/%Y'))
49+
# Delete superseded data
50+
#puts period.content_removal_requests.joins(:product).where('products.name' => r[3]).count
51+
#period.content_removal_requests.joins(:product).where('products.name' => r[3]).delete_all
52+
# Create new data
53+
e = ContentRemovalRequest.new(
54+
:court_orders => r[5],
55+
:executive => r[6],
56+
:items => r[7]
57+
)
58+
e.content_removal_request_period = period
59+
e.product = Product.find_or_create_by_name(r[3])
60+
e.reason = Reason.find_or_create_by_name(r[4])
61+
e.save!
62+
end
63+
end
64+
2265
desc "Run all transparencyreport data tasks"
23-
task :all => [:load_content_removal_requests]
66+
task :all => [
67+
:load_content_removal_requests,
68+
:load_content_removal_requests_by_product,
69+
:load_content_removal_requests_by_product_and_reason
70+
]
2471

2572
end
2673

test/fixtures/content_removal_requests.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ one:
22
content_removal_request_period_id: 1
33
product_id: 1
44
reason_id: 1
5-
courtOrders: 1
5+
court_orders: 1
66
executive: 1
77
items: 1
88

99
two:
1010
content_removal_request_period_id: 1
1111
product_id: 1
1212
reason_id: 1
13-
courtOrders: 1
13+
court_orders: 1
1414
executive: 1
1515
items: 1

0 commit comments

Comments
 (0)