Created
October 19, 2012 21:08
-
-
Save alvaro-cuesta/3920748 to your computer and use it in GitHub Desktop.
MV Quote Fetcher
This file contains hidden or 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
request = require 'request' | |
express = require 'express' | |
app = express() | |
############ | |
## CONFIG ## | |
############ | |
PORT = 3000 | |
#USER = 'xxx' | |
#PASS = 'xxx' | |
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4' | |
############### | |
## CONSTANTS ## | |
############### | |
MV_LOGIN = 'http://www.mediavida.com/login' | |
MV_THREAD = (forum, thread) -> "http://www.mediavida.com/foro/#{forum}/#{thread}" | |
MV_QUOTE = 'http://www.mediavida.com/foro/post_quote.php' | |
TOKEN_REGEX = /<input type="hidden" name="token" id="token" value="(.*?)">/g | |
TID_REGEX = /<input type="hidden" id="tid" value="(.*?)">/g | |
# | |
fetch_quote = (num, tid, token, callback) -> | |
request.post | |
url: MV_QUOTE | |
headers: | |
'User-Agent': USER_AGENT | |
'X-Requested-With': 'XMLHttpRequest' | |
form: | |
num: num | |
tid: tid | |
token: token | |
(err, res, body) -> | |
if err | |
console.error "ERROR FETCHING QUOTE #{url}\n", err | |
return | |
quote = JSON.parse(body) | |
if quote.status == 0 | |
console.error "QUOTE ERROR: #{quote.full}", err | |
return | |
callback quote | |
# | |
fetch_thread_data = (thread, forum, callback) -> | |
url = if forum then MV_THREAD(forum, thread) else thread | |
console.log "Fetching: #{url}" | |
request.get url, (err, res, body) -> | |
if err | |
console.error "ERROR: Couldn't fetch thread #{url}\n" | |
console.log err | |
return | |
tid_match = TID_REGEX.exec body | |
TID_REGEX.lastIndex = 0 | |
if not tid_match | |
console.error "ERROR: Couldn't find \"tid\" in #{url}" | |
console.log body | |
return | |
token_match = TOKEN_REGEX.exec body | |
TOKEN_REGEX.lastIndex = 0 | |
if not token_match | |
console.error "ERROR: Couldn't find \"token\" in #{url}" | |
console.log body | |
return | |
token = token_match[1] | |
tid = tid_match[1] | |
console.log "Token: #{token}" | |
console.log "Thread ID: #{tid}\n" | |
callback tid, token | |
# | |
login = (user, pass, callback) -> | |
request.post | |
url: MV_LOGIN | |
form: | |
name: user | |
password: pass | |
cookie: 1 | |
(err, res, body) -> | |
if err | |
console.error "ERROR LOGGING IN\n", err | |
return | |
console.log "-- LOGGED IN AS #{USER} --" | |
callback() | |
# | |
##### | |
app.get '/', (req, res) -> | |
num = req.query.num | |
thread = req.query.thread | |
forum = req.query.forum | |
fetch_thread_data thread, forum, (tid, token) -> | |
fetch_quote num, tid, token, (quote) -> | |
res.send quote.full | |
#login USER, PASS, () -> | |
# app.listen PORT | |
app.listen PORT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment