Skip to content

Instantly share code, notes, and snippets.

@sai43
Last active June 3, 2019 22:20
Show Gist options
  • Save sai43/0968c3d690ef8da73bd2 to your computer and use it in GitHub Desktop.
Save sai43/0968c3d690ef8da73bd2 to your computer and use it in GitHub Desktop.
api for funcheap - fetch events from funcheap
require 'chronic'
require 'nokogiri'
require 'open-uri'
require 'date'
require 'json'
require 'paperclip'
namespace :evee do
desc "Fetch Events from Funcheap"
task pull_funcheap_events: :environment do
def duplicate_event(uniqkey)
event = Event.where(uniqkey: uniqkey)
return event.present? ? true : false
end
def event_parse(url)
return nil unless url
doc = Nokogiri::XML(open(url))
event_info = doc.css('span.left span script')
if event_info[5]
maps_raw = event_info[5].text
maps_json = maps_raw[maps_raw.index('{'), maps_raw.rindex('}]}') - maps_raw.index('{') + 3]
loc_info = JSON.parse(maps_json)
image = doc.css('img.alignright')
coordinates = loc_info["pois"][0]["point"]
address = loc_info["pois"][0]["address"]
return [coordinates, address]
end
end
required_date = Date.new(2015,10,1)
month_start_date = required_date.beginning_of_month #|| Date.today.beginning_of_month
month_end_date = required_date.end_of_month # || Date.today.end_of_month
(month_start_date).upto(month_end_date) do |d|
begin
puts eventlist_url = "http://sf.funcheap.com/#{d.strftime("%Y/%m/%d")}/"
page = Nokogiri::HTML(open(eventlist_url))
title_parse = page.css('div.tanbox.left')
title_parse.each do |event|
if event.css('span.title').css('a')[0] != nil
coordinates, address, image = event_parse(event.css('span.title').css('a')[0]["href"])
events_array = event.css('span.title').css('a').text.split(" | ")
date_time = event.css('div.meta').text
misc = date_time[0, date_time.index("|")]
time_extract = Chronic.parse(date_time[0, date_time.index("|")])
descrip_text = (event.css('p').text)
d_string = event.children.last.text.gsub("\r", "").gsub("\n", "").gsub("\t", "").strip
d_blurb = if descrip_text == "" then d_string else descrip_text end
event_name = events_array[0]
event_description = d_blurb.gsub("\n", " ")
event_url = event.css('a')[0]["href"]
event_image_url = event.css('img')[0]["src"]
uniqkey = event_name.split.join + "-" + d.strftime("%Y%m%d")
unless duplicate_event(uniqkey)
event = Event.new(:title => event_name,
:description => event_description.present? ? event_description : "No Description Available",
:url => event_url.present? ? event_url : "http://sf.funcheap.com",
:event_image => event_image_url.present? ? URI.parse(event_image_url).open : URI.parse("http://dnh7crmrr8pwi.cloudfront.net/images/events/574.large.png?1441967456").open,
:venue => address.present? ? address : "All Over San Francisco",
:address => "San Francisco, CA",
:latitude => coordinates.present? ? coordinates["lat"].to_f : 37.773972,
:longitude => coordinates.present? ? coordinates["lng"].to_f : -122.431297,
:source => "Funcheap",
:price => 0,
:city => "San Francisco",
:moods => ["social"],
:startdate => d.strftime("%Y/%m/%d").gsub('/','-'),
:enddate => d.strftime("%Y/%m/%d").gsub('/','-'),
:uniqkey => uniqkey
)
event.save
puts "objectId: #{event.id} - #{event.title} - #{event.event_image.url}"
else
puts "Event already exist"
end #unless close
end # if close
end #title-parse do close
rescue OpenURI::HTTPError => e
if e.message == '500 Internal Server Error'
next
else
raise e
end
end #begin close
end #upto loop close
end #end task
end #end namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment