Skip to content

Commit

Permalink
Remove java class
Browse files Browse the repository at this point in the history
  • Loading branch information
alikemal.ocalan committed Jan 16, 2021
1 parent 6bbd56a commit 374ffce
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 86 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import com.typesafe.config.ConfigFactory
trait Config {
private val config = ConfigFactory.load

val userName: String = config.getString("username")
val passWord: String = config.getString("password")
protected val userName: String = config.getString("username")
protected val passWord: String = config.getString("password")

val bucketName: String = config.getString("bucket-name")
val region: String = config.getString("region")
val s3Endpoint: String = config.getString("s3-endpoint")
protected val bucketName: String = config.getString("bucket-name")
protected val region: String = config.getString("region")
protected val s3Endpoint: String = config.getString("s3-endpoint")

val enableS3Backup: Boolean = config.getBoolean("enable-s3-backup")
val clientS3SettingPath = "setting/igclient.ser"
val cookieS3SettingPath = "setting/cookie.ser"
val clientSettingTMPPath = s"${System.getProperty("java.io.tmpdir")}/igclient.ser"
val cookieSettingTMPPath = s"${System.getProperty("java.io.tmpdir")}/cookie.ser"
protected val enableS3Backup: Boolean = config.getBoolean("enable-s3-backup")
protected val clientS3SettingPath = "setting/igclient.ser"
protected val cookieS3SettingPath = "setting/cookie.ser"
protected val clientSettingTMPPath = s"${System.getProperty("java.io.tmpdir")}/igclient.ser"
protected val cookieSettingTMPPath = s"${System.getProperty("java.io.tmpdir")}/cookie.ser"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.alikemalocalan.instastorysaver.model

import com.github.alikemalocalan.instastorysaver.Utils
import okhttp3.{Cookie, CookieJar, HttpUrl}

import java.io.{ObjectInputStream, ObjectOutputStream}
import java.util
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
import scala.jdk.CollectionConverters._

class SerializableCookieJar extends CookieJar with Serializable {

val map: mutable.Map[String, ListBuffer[SerializableCookie]] = mutable.Map[String, ListBuffer[SerializableCookie]]()

override def loadForRequest(httpUrl: HttpUrl): util.List[Cookie] = {
map.getOrElse(httpUrl.host(), ListBuffer[SerializableCookie]().empty)
.map(c => c.cookie)
.asJava
}

override def saveFromResponse(httpUrl: HttpUrl, list: java.util.List[Cookie]): Unit = {
val cookies = list.asScala.map(SerializableCookie).to(ListBuffer)

if (map.contains(httpUrl.host())) {
map(httpUrl.host()).addAll(cookies)
} else map.put(httpUrl.host(), cookies)
}

case class SerializableCookie(var cookie: Cookie) extends Serializable {

private def writeObject(out: ObjectOutputStream): Unit = {
Utils.writeObject(cookie, out)
}

private def readObject(in: ObjectInputStream): Unit = {
cookie = Utils.readObject(in)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import scala.collection.convert.ImplicitConversions._
object InstaService {
val logger: Log = LogFactory.getLog(this.getClass)

def getFollowingUsers(implicit client: IGClient): Stream[User] = {
client.getActions.users().findByUsername(getAccountUserName).thenApply[Stream[User]] { user =>
def getFollowingUsers(implicit client: IGClient): LazyList[User] = {
client.getActions.users().findByUsername(getAccountUserName).thenApply[LazyList[User]] { user =>
user.followingFeed()
.toStream
.to(LazyList)
.flatMap(user => user.getUsers.map { user =>
User(user.getUsername, user.getPk)
})
Expand All @@ -35,17 +35,17 @@ object InstaService {
LoginService.serializeLogin(userName, password)
}

def getStoriesAllFollowing(users: Stream[User])(implicit client: IGClient): Stream[UserStories] =
def getStoriesAllFollowing(users: LazyList[User])(implicit client: IGClient): LazyList[UserStories] =
users.map(user => InstaService.getUserStory(user).join())

def getFeedsAllFollowing(users: Stream[User])(implicit client: IGClient): Stream[UserFeedMedias] =
def getFeedsAllFollowing(users: LazyList[User])(implicit client: IGClient): LazyList[UserFeedMedias] =
users.map(user => InstaService.getUserFeed(user).join())

def getUserHighLightStoriesAllFollowing(users: Stream[User])(implicit client: IGClient): Stream[UserHighLightStoryMedias] =
def getUserHighLightStoriesAllFollowing(users: LazyList[User])(implicit client: IGClient): LazyList[UserHighLightStoryMedias] =
users.map(user => InstaService.getUserHighLightStory(user).join())

def saveStories(enableS3: Boolean, defaultFolder: Option[String] = None)(implicit client: IGClient): Unit = {
val users: Stream[UserStories] = getStoriesAllFollowing(InstaService.getFollowingUsers)
val users: LazyList[UserStories] = getStoriesAllFollowing(InstaService.getFollowingUsers)

val operations = users
.flatMap { userStories =>
Expand Down Expand Up @@ -112,8 +112,8 @@ object InstaService {
private def getUserStory(user: User)(implicit client: IGClient): CompletableFuture[UserStories] =
new FeedUserStoryRequest(user.userId).execute(client).thenApply[UserStories] { userStory =>
val stories = if (userStory.getReel != null)
userStory.getReel.getItems.flatMap(reelMediaToUserStories)
else List()
userStory.getReel.getItems.flatMap(reelMediaToUserStories).toSeq
else Seq()
UserStories(user, stories)
}.exceptionally { _ =>
UserStories(user, List())
Expand All @@ -122,7 +122,7 @@ object InstaService {
private def getUserFeed(user: User)(implicit client: IGClient): CompletableFuture[UserFeedMedias] =
new FeedUserRequest(user.userId).execute(client).thenApply[UserFeedMedias] { feed =>
val feedList = feed.getItems.flatMap(timelineMediaToUserFeeds)
UserFeedMedias(user, feedList)
UserFeedMedias(user, feedList.toSeq)
}.exceptionally { _ =>
UserFeedMedias(user, List())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.github.alikemalocalan.instastorysaver.service

import com.github.alikemalocalan.instastorysaver.Config
import com.github.alikemalocalan.instastorysaver.Utils.{deserialize, serialize}
import com.github.alikemalocalan.instastorysaver.model.SerializableCookieJar
import com.github.alikemalocalan.instastorysaver.service.S3ClientService._
import com.github.alikemalocalan.instastorysaver.{Config, SerializableCookieJar}
import com.github.instagram4j.instagram4j.IGClient
import com.github.instagram4j.instagram4j.utils.IGUtils
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -38,7 +39,7 @@ object LoginService extends Config {

}

def formTestHttpClient(jar: SerializableCookieJar): OkHttpClient =
private def formTestHttpClient(jar: SerializableCookieJar): OkHttpClient =
IGUtils.defaultHttpClientBuilder.cookieJar(jar).build

private def getSavingClientSettingFiles: (File, File) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ object S3ClientService extends Config {
file
}

def upload(operations: Stream[UrlOperation], enableS3: Boolean, defaultFolder: Option[String] = None): Unit =
def upload(operations: LazyList[UrlOperation], enableS3: Boolean, defaultFolder: Option[String] = None): Unit =
operations.foreach { operation =>
logger.info(operation)
val file = downloadUrl(operation.url)
if (enableS3) {
uploadToS3(file, operation.fileFullPath)
} else FileUtils.moveFile(file, new File(s"${defaultFolder.get}/${operation.fileFullPath}"))
} else {
val destinationFile = new File(s"${defaultFolder.get}/${operation.fileFullPath}")
if (!destinationFile.exists())
FileUtils.moveFile(file, destinationFile)
}
file.delete()
}

Expand Down

0 comments on commit 374ffce

Please sign in to comment.