mirror of
https://github.com/twitter/the-algorithm.git
synced 2024-12-22 18:21:51 +01:00
Merge 37ec1935ea
into 72eda9a24f
This commit is contained in:
commit
d014df4370
@ -1,7 +1,6 @@
|
|||||||
package com.twitter.follow_recommendations.common.clients.geoduck
|
package com.twitter.follow_recommendations.common.clients.geoduck
|
||||||
|
|
||||||
import com.twitter.follow_recommendations.common.models.GeohashAndCountryCode
|
import com.twitter.follow_recommendations.common.models.GeohashAndCountryCode
|
||||||
import com.twitter.geoduck.common.thriftscala.Location
|
|
||||||
import com.twitter.geoduck.common.thriftscala.PlaceQuery
|
import com.twitter.geoduck.common.thriftscala.PlaceQuery
|
||||||
import com.twitter.geoduck.common.thriftscala.ReverseGeocodeIPRequest
|
import com.twitter.geoduck.common.thriftscala.ReverseGeocodeIPRequest
|
||||||
import com.twitter.geoduck.service.thriftscala.GeoContext
|
import com.twitter.geoduck.service.thriftscala.GeoContext
|
||||||
@ -12,16 +11,10 @@ import javax.inject.Singleton
|
|||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class ReverseGeocodeClient @Inject() (rgcService: ReverseGeocoder.MethodPerEndpoint) {
|
class ReverseGeocodeClient @Inject() (rgcService: ReverseGeocoder.MethodPerEndpoint) {
|
||||||
def getGeohashAndCountryCode(ipAddress: String): Stitch[GeohashAndCountryCode] = {
|
def getGeohashAndCountryCode(ipAddress: String): Stitch[GeohashAndCountryCode] =
|
||||||
Stitch
|
Stitch.callFuture {
|
||||||
.callFuture {
|
rgcService.reverseGeocodeIp(
|
||||||
rgcService
|
ReverseGeocodeIPRequest(Seq(ipAddress), PlaceQuery(None), simpleReverseGeocode = true)
|
||||||
.reverseGeocodeIp(
|
|
||||||
ReverseGeocodeIPRequest(
|
|
||||||
Seq(ipAddress),
|
|
||||||
PlaceQuery(None),
|
|
||||||
simpleReverseGeocode = true
|
|
||||||
) // note: simpleReverseGeocode means that country code will be included in response
|
|
||||||
).map { response =>
|
).map { response =>
|
||||||
response.found.get(ipAddress) match {
|
response.found.get(ipAddress) match {
|
||||||
case Some(location) => getGeohashAndCountryCodeFromLocation(location)
|
case Some(location) => getGeohashAndCountryCodeFromLocation(location)
|
||||||
@ -29,29 +22,10 @@ class ReverseGeocodeClient @Inject() (rgcService: ReverseGeocoder.MethodPerEndpo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private def getGeohashAndCountryCodeFromLocation(location: Location): GeohashAndCountryCode = {
|
private def getGeohashAndCountryCodeFromLocation(location: Location): GeohashAndCountryCode = {
|
||||||
val countryCode: Option[String] = location.simpleRgcResult.flatMap { _.countryCodeAlpha2 }
|
val countryCode = location.simpleRgcResult.flatMap(_.countryCodeAlpha2)
|
||||||
|
val geohashString = location.geohash.flatMap(_.stringGeohash.map(_.take(4)))
|
||||||
val geohashString: Option[String] = location.geohash.flatMap { hash =>
|
|
||||||
hash.stringGeohash.flatMap { hashString =>
|
|
||||||
Some(ReverseGeocodeClient.truncate(hashString))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GeohashAndCountryCode(geohashString, countryCode)
|
GeohashAndCountryCode(geohashString, countryCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
object ReverseGeocodeClient {
|
|
||||||
|
|
||||||
val DefaultGeoduckIPRequestContext: GeoContext =
|
|
||||||
GeoContext(allPlaceTypes = true, includeGeohash = true, includeCountryCode = true)
|
|
||||||
|
|
||||||
// All these geohashes are guessed by IP (Logical Location Source).
|
|
||||||
// So take the four letters to make sure it is consistent with LocationServiceClient
|
|
||||||
val GeohashLengthAfterTruncation = 4
|
|
||||||
def truncate(geohash: String): String = geohash.take(GeohashLengthAfterTruncation)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user