Skip to content

Instantly share code, notes, and snippets.

@moonhouse
Created June 13, 2013 09:44
Show Gist options
  • Select an option

  • Save moonhouse/5772512 to your computer and use it in GitHub Desktop.

Select an option

Save moonhouse/5772512 to your computer and use it in GitHub Desktop.
Import RSS entries to TV4 Intranet
CREATE TABLE importedentries (uri varchar, saved datetime);
#encoding: UTF-8
require 'httparty'
require 'nokogiri'
require 'sqlite3'
require 'date'
require 'mechanize'
m = Mechanize.new
m.add_auth('https://intranet.tv4.se/','david.hall','jättehemligt',nil,'tv4.se')
db = SQLite3::Database.new( "imported.db" )
uri = 'http://http.tv4.se/feed/'
response = HTTParty.get(uri)
entries = response.parsed_response
entries['rss']['channel']['item'].each do |entry|
rows = db.execute( "select count(*) from importedentries where uri = ?", entry['guid']['__content__'] )
if rows[0][0] == 0
title = entry['title']
content = entry['encoded']
page = Nokogiri::HTML(content)
page.xpath('//img').each do |image|
width = image['width'].to_f
height = image['height'].to_f
ratio = width/height
if width > 460
new_width = 460
new_height = (460/ratio).round
else
new_width = width
new_height = height
end
image['width'] = new_width
image['height'] = new_height
end
puts page.to_html
intranet_page = m.get('https://intranet.tv4.se/')
intranet_page = m.get('https://intranet.tv4.se/publikt/Nyhetsbloggar/httptv4se/')
form = intranet_page.forms.first
form['__EVENTTARGET'] = 'ctl00$mainContent$btnPublish'
form['ctl00$mainContent$propNewEntryName$ctl00$ctl00'] = title
form['ctl00$mainContent$propNewEntryMainBody'] = page.to_html
intranet_page = form.submit
db.execute( "insert into importedentries values ( ?, ? )", entry['guid']['__content__'], Time.now.to_s)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment