-
Notifications
You must be signed in to change notification settings - Fork 5
Add option to track multiple pages in one stream #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,23 +10,35 @@ import org.apache.spark.streaming.StreamingContext | |
| import org.apache.spark.streaming.dstream.ReceiverInputDStream | ||
|
|
||
| object FacebookUtils { | ||
| def createPageStream( | ||
| def createPageStreams( | ||
| ssc: StreamingContext, | ||
| auth: FacebookAuth, | ||
| pageId: String, | ||
| pageIds: Set[String], | ||
| fields: Set[String] = Set("message", "place", "caption", "from", "name", "comments"), | ||
| pollingSchedule: PollingSchedule = PollingSchedule(30, TimeUnit.SECONDS), | ||
| pollingWorkers: Int = 1, | ||
| storageLevel: StorageLevel = StorageLevel.MEMORY_ONLY | ||
| ): ReceiverInputDStream[FacebookPost] = { | ||
| new FacebookInputDStream( | ||
| ssc = ssc, | ||
| client = new FacebookPageClient( | ||
| clients = pageIds.map(pageId => new FacebookPageClient( | ||
| pageId = pageId, | ||
| auth = auth, | ||
| fields = fields), | ||
| fields = fields)), | ||
| pollingSchedule = pollingSchedule, | ||
| pollingWorkers = pollingWorkers, | ||
| storageLevel = storageLevel) | ||
| } | ||
|
|
||
| def createPageStream( | ||
| ssc: StreamingContext, | ||
| auth: FacebookAuth, | ||
| pageId: String, | ||
| fields: Set[String] = Set("message", "place", "caption", "from", "name", "comments"), | ||
| pollingSchedule: PollingSchedule = PollingSchedule(30, TimeUnit.SECONDS), | ||
| pollingWorkers: Int = 1, | ||
| storageLevel: StorageLevel = StorageLevel.MEMORY_ONLY | ||
| ): ReceiverInputDStream[FacebookPost] = { | ||
| createPageStreams(ssc, auth, Set(pageId), fields, pollingSchedule, pollingWorkers, storageLevel) | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we're going to need to expose a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. Will do in a follow-up PR, see #3 for tracking. |
||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,78 @@ | ||
| package com.github.catalystcode.fortis.spark.streaming.facebook.client | ||
|
|
||
| import java.util | ||
| import java.util.Date | ||
|
|
||
| import com.github.catalystcode.fortis.spark.streaming.facebook.FacebookAuth | ||
| import facebook4j.{Post, ResponseList} | ||
| import com.github.catalystcode.fortis.spark.streaming.facebook.dto.FacebookPost | ||
| import com.github.catalystcode.fortis.spark.streaming.facebook.{FacebookAuth, Logger} | ||
| import facebook4j._ | ||
| import facebook4j.auth.AccessToken | ||
|
|
||
| import collection.JavaConverters._ | ||
| import scala.collection.mutable.ListBuffer | ||
|
|
||
| @SerialVersionUID(100L) | ||
| class FacebookPageClient( | ||
| pageId: String, | ||
| auth: FacebookAuth, | ||
| fields: Set[String]) | ||
| extends FacebookClient(auth, fields) { | ||
| extends Serializable with Logger { | ||
|
|
||
| @transient protected lazy val facebook: Facebook = createFacebook() | ||
| @transient private lazy val defaultLookback = new Date(new Date().getTime - 2 * 3600000 /* two hours */) | ||
| @transient private lazy val defaultFields = Set("permalink_url", "created_time") | ||
|
|
||
| def loadNewFacebooks(after: Option[Date] = None): Iterable[FacebookPost] = { | ||
| val allPosts = ListBuffer[FacebookPost]() | ||
|
|
||
| try { | ||
| var posts = fetchFacebookResponse(after.getOrElse(defaultLookback)) | ||
| while (posts != null && posts.getPaging != null) { | ||
| posts.asScala.foreach(post => { | ||
| allPosts += FacebookPost(pageId, post, fetchComments(post)) | ||
| }) | ||
| logDebug(s"Got another page: ${Option(posts.getPaging.getNext).getOrElse("no")}") | ||
| posts = facebook.fetchNext(posts.getPaging) | ||
| } | ||
| } catch { | ||
| case fbex: FacebookException => | ||
| logError("Problem fetching response from Facebook", fbex) | ||
| } | ||
|
|
||
| allPosts | ||
| } | ||
|
|
||
| private def fetchComments(post: Post): Seq[Comment] = { | ||
| val allComments = new util.ArrayList[Comment]() | ||
|
|
||
| try { | ||
| var comments = post.getComments | ||
| while (comments != null && comments.getPaging != null) { | ||
| allComments.addAll(comments) | ||
| logDebug(s"Got another comments page: ${Option(comments.getPaging.getNext).getOrElse("no")}") | ||
| comments = facebook.fetchNext(comments.getPaging) | ||
| } | ||
| } catch { | ||
| case fbex: FacebookException => | ||
| logError(s"Problem fetching comments from Facebook for post ${post.getPermalinkUrl}", fbex) | ||
| } | ||
|
|
||
| allComments.asScala | ||
| } | ||
|
|
||
| private def createFacebook(): Facebook = { | ||
| val facebook = new FacebookFactory().getInstance() | ||
| facebook.setOAuthAppId(auth.appId, auth.appSecret) | ||
| facebook.setOAuthAccessToken(new AccessToken(auth.accessToken, null)) | ||
| } | ||
|
|
||
| protected def createReading(after: Date): Reading = { | ||
| // todo: reduce limit if we got a too-large-request error | ||
| new Reading().since(after).fields((defaultFields ++ fields).toArray : _*).limit(100) | ||
| } | ||
|
|
||
| override def fetchFacebookResponse(after: Date): ResponseList[Post] = { | ||
| protected def fetchFacebookResponse(after: Date): ResponseList[Post] = { | ||
| logDebug(s"Fetching posts for $pageId since $after") | ||
| facebook.getPosts(pageId, createReading(after)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens to the comments that were posted since
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Posts and Comments are linked in the Facebook API. As such, you can get a post for date T and then look up its linked comments for it that may have been posted at a date after T. |
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| version := "0.0.1" | ||
| version := "0.0.2" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove comments as this should be populated from
createPageCommentsStreamThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now implemented in #4.