Last active
February 20, 2016 21:48
-
-
Save krsoninikhil/a4913750772439f8ae49 to your computer and use it in GitHub Desktop.
It will post on Facebook about the new companies opened to your branch for internship on Channeli. Needs IITR intranet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
ruby "2.2.3" | |
# following gems are required in the main script. | |
gem "koala", "~> 2.2" | |
gem "httparty" | |
gem "nokogiri" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 160217 | |
# to make http requests | |
require 'httparty' | |
# to parse html response | |
require 'nokogiri' | |
# to post on facebook wall using GraphAPI | |
require 'koala' | |
# to parse the json data | |
require 'json' | |
# getting details from Channeli | |
# to get your CHANNELI_SESSID, login to Channeli and check cookies | |
# since you can only apply to the companies opened for your branch, this script will post only about them. | |
CHANNELI_SESSID = '<chenneli-sessid>' | |
BASE_URI = 'https://channeli.in' | |
print "Connecting to Channeli..." | |
@response = HTTParty.get(BASE_URI + '/internship/company/list', cookies: { | |
'CHANNELI_SESSID': CHANNELI_SESSID | |
}) | |
puts "#{@response.code} #{@response.message}" | |
# puts @response.headers.inspect | |
if @response.code == 200 | |
print "Connected. Fetching data..." | |
# lets dig into HTML response | |
html = Nokogiri::HTML(@response.body) | |
rows = html.search('table#t1 tbody').children.search('tr') | |
data = Hash.new | |
data[:company_count] = rows.length | |
data[:company_name] = Array.new | |
data[:last_date] = Array.new | |
data[:pre_posted] = Array.new | |
# check if data is already posted | |
if File.exist?('tpo_data.json') | |
data_file = JSON.parse(File.read('tpo_data.json')) | |
pre_posted = data_file['company_name'] | |
else | |
pre_posted = Array.new | |
end | |
# counting only new companies opened | |
new_companies = data[:company_count] - pre_posted.length | |
company = new_companies == 1 ? 'company' : 'companies' | |
rows.each_with_index do |row, i| | |
data[:company_name] << row.children.search('td')[1].content.strip | |
data[:last_date] << row.children.search('td')[2].content.strip | |
if pre_posted.include?(data[:company_name][i]) | |
data[:pre_posted] << true | |
else | |
data[:pre_posted] << false | |
end | |
end | |
puts "Done. Total #{data[:company_count]} #{company} found." | |
# posting on facebook | |
if data[:pre_posted].include?(false) | |
print "Posting on Facebook about #{new_companies} new #{company}..." | |
# follow these instructions to get access token for two months http://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token | |
access_token = '<access-token>' | |
# this facebook user will be used to post the content, so make sure it don't have friends from outside the campus. | |
# creating post | |
post = "#{new_companies} new #{company} opened for us:\r\n" | |
serial_no = 1 | |
data[:company_count].times do |i| | |
unless data[:pre_posted][i] | |
post += "- #{data[:company_name][i]} (Last Date: #{data[:last_date][i]})\r\n" | |
serial_no += 1 | |
end | |
end | |
post += "Hurry up!!\r\nhttps://channeli.in#notices" | |
# using koala gem to post | |
@graph = Koala::Facebook::API.new(access_token) | |
@graph.put_wall_post(post) | |
puts "Done." | |
# save it in a json file | |
print "saving data to json file..." | |
File.open('tpo_data.json', 'w') do |f| | |
f.write(data.to_json) | |
end | |
else | |
puts "No new company arrived. Nothing to post on Facebook." | |
end | |
puts "Done." | |
else | |
puts "Error: Cannot not connect to Channeli!" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment