Skip to content

Commit

Permalink
use needle to replace request & bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
breezewish committed Oct 30, 2013
1 parent 182a326 commit 307ab76
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 186 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -15,4 +15,5 @@ npm-debug.log
node_modules

/ingress-exporter.log
/config.coffee
/config.coffee
/build/config.js
80 changes: 13 additions & 67 deletions build/lib/request.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 9 additions & 12 deletions build/lib/tile.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -10,7 +10,7 @@
"grunt-contrib-coffee": "*",
"grunt-contrib-watch": "*",
"grunt-contrib-copy": "*",
"request": "*",
"needle": "*",
"mongoskin": "*",
"moment": "*",
"winston": "*",
Expand Down
71 changes: 12 additions & 59 deletions src/lib/request.coffee
@@ -1,7 +1,4 @@
r = require 'request'
zlib = require 'zlib'
jar = r.jar()
req = r.defaults jar:jar
needle = require 'needle'

# Request pools
pool = []
Expand All @@ -11,18 +8,15 @@ reqCount = 0
cookies = {}

for v in Config.Auth.CookieRaw.split(';')

v = v.trim()

C = v.split '='
C = v.trim().split '='
cookies[C[0]] = unescape C[1]
jar.add r.cookie(v)

Request = GLOBAL.Request =

add: (options) ->

activeMunge = Config.Munges.Data[Config.Munges.ActiveSet]

methodName = 'dashboard.' + options.action
versionStr = 'version_parameter'

Expand Down Expand Up @@ -55,76 +49,35 @@ sendRequest = ->
v = pool.shift()
reqCount++

buffer = []
bodyLen = 0

v.request || v.request()

req
url: 'http://www.ingress.com/r/' + v.m
method: 'POST'
body: JSON.stringify v.d
encoding: null
needle.post 'http://www.ingress.com/r/' + v.m, JSON.stringify(v.d),
compressed: true
headers:
# user-agent is essential for GZIP response here
'Accept': 'application/json, text/javascript, */*; q=0.01'
'Accept-Encoding': 'gzip,deflate'
'Accept-Language': 'zh-CN,zh;q=0.8'
'Content-type': 'application/json; charset=utf-8'
'Cookie': Config.Auth.CookieRaw
'Host': 'www.ingress.com'
'Origin': 'http://www.ingress.com'
'Referer': 'http://www.ingress.com/intel'
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36'
'X-CSRFToken': cookies.csrftoken
.on 'error', (err) ->
# may produce multiple errors here
return if v.errorEmited?

reqCount--
Request.requested++
v.errorEmited = true
v.error && v.error err
v.response && v.response err
.pipe zlib.createGunzip()
.on 'error', (err) ->
return if v.errorEmited?
, (error, response, body) ->

reqCount--
Request.requested++
v.errorEmited = true
v.error && v.error err
v.response && v.response err
.on 'data', (chunk) ->
buffer.push chunk
bodyLen += chunk.length
.on 'end', ->

reqCount--
Request.requested++

body = new Buffer bodyLen
i = 0
for chunk in buffer
chunk.copy body, i, 0, chunk.length
i += chunk.length

body = body.toString()
if error
v.error && v.error error
v.response && v.response error
return

# not authorized
if body is 'User not authenticated'
logger.error '[Auth] Authorize failed. Please update the cookie.'
process.exit 0
return

try
body = JSON.parse body
catch err
return if v.errorEmited?
v.errorEmited = true
v.error && v.error err
v.response && v.response err
return


v.success && v.success body
v.response && v.response null

Expand Down
20 changes: 7 additions & 13 deletions src/lib/tile.coffee
Expand Up @@ -20,7 +20,7 @@ TileBucket = GLOBAL.TileBucket =

TileBucket.bucket.push tileId if tileId?

if TileBucket.bucket.length >= Config.TileBucket.Max
if TileBucket.bucket.length >= Config.TileBucket.Max or not tileId?

TileBucket.request TileBucket.bucket, callback
TileBucket.bucket = []
Expand All @@ -45,7 +45,7 @@ TileBucket = GLOBAL.TileBucket =
TileBucket.requestCount++
requestId = TileBucket.requestCount

data = boundsParamsList: boundsParamsList
data = quadKeys: boundsParamsList

Request.add

Expand All @@ -57,7 +57,7 @@ TileBucket = GLOBAL.TileBucket =

onError: (err) ->

logger.error "[Request] ErrorCode=#{err.code}"
logger.error "[Request] " + err
processErrorTileResponse tileIds

afterResponse: ->
Expand Down Expand Up @@ -85,7 +85,7 @@ TileBucket = GLOBAL.TileBucket =

if Tile.data[tileId].status isnt STATUS_COMPLETE

boundsParamsList.push Tile.bounds[tileId]
boundsParamsList.push Tile.bounds[tileId].id
Tile.data[tileId].status = STATUS_PENDING

Database.db.collection('Tiles').update
Expand Down Expand Up @@ -131,14 +131,8 @@ Tile = GLOBAL.Tile =
for y in [y1 .. y2]
for x in [x1 .. x2]

tileId = Utils.pointToTileId Config.MinPortalLevel, x, y
latNorth = Utils.tileToLat y, Config.MinPortalLevel
latSouth = Utils.tileToLat y+1, Config.MinPortalLevel
lngWest = Utils.tileToLng x, Config.MinPortalLevel
lngEast = Utils.tileToLng x+1, Config.MinPortalLevel

boundsParams = Utils.generateBoundsParams tileId, latSouth, lngWest, latNorth, lngEast
tileBounds.push boundsParams
tileId = Utils.pointToTileId Config.MinPortalLevel, x, y
tileBounds.push id: tileId

return tileBounds

Expand Down Expand Up @@ -203,7 +197,7 @@ Tile = GLOBAL.Tile =

_prepareTiles: (callback) ->

logger.success "[Tile] Prepared #{Tile.length} tiles"
logger.info "[Tile] Prepared #{Tile.length} tiles"

Database.db.collection('Tiles').ensureIndex [['status', 1]], false, ->

Expand Down

0 comments on commit 307ab76

Please sign in to comment.