Commit 66ae2551 authored by soheib's avatar soheib
Browse files

add website sells calculation

parent 01f2596b
......@@ -13,12 +13,13 @@ import { LottoryResultSchema } from './instagram/models/LottoryResult.schema'
import { PostSchema } from './instagram/models/post.schema'
import { StoryMentionSchema } from './instagram/models/storyMention.schema'
import { ScheduleModule } from '@nestjs/schedule'
import { WebsiteSellSchema } from './instagram/models/websiteSell.schema';
@Module({
imports: [
ScheduleModule.forRoot(),
MongooseModule.forRoot(
'mongodb://instagram:wcD3B5sGw0yQ@185.231.180.248:27017/instagram-lottry?authSource=admin&authMechanism=SCRAM-SHA-256&connectTimeoutMS=10000&readPreference=primary&serverSelectionTimeoutMS=5000&appname=MongoDB%20Compass&directConnection=true&ssl=false',
'mongodb://localhost:27017/instagram-lottry',
),
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
......@@ -42,6 +43,9 @@ import { ScheduleModule } from '@nestjs/schedule'
MongooseModule.forFeature([
{ name: 'StoryMention', schema: StoryMentionSchema },
]),
MongooseModule.forFeature([
{ name: 'WebsiteSell', schema: WebsiteSellSchema },
]),
InstagramModule,
......
import { ApiProperty } from "@nestjs/swagger"
export class AddWebsiteSellsDto {
@ApiProperty({ example: [{ username: "soheib", value: 5 }, { username: "sohasd.eib", value: 4 }] })
website_sells: Array<{ username: string, value: number }>
}
\ No newline at end of file
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
import { ApiBody, ApiTags } from '@nestjs/swagger'
import { AddStoryDataDto } from './dto/add-story-data-dto'
import { AddWebsiteSellsDto } from './dto/add-website-sell-dto'
import { GetCommentsDto } from './dto/get-comments-dto'
import { GetFollowersDto } from './dto/get-followers-dto'
import { GetLikesDto } from './dto/get-likes-dto'
......@@ -11,7 +12,7 @@ export class InstagramController {
constructor(private instagramService: InstagramService) { }
@Get('get-json-followers')
async getJsonDataFollowers() {
await this.instagramService.getFollowersFromJsonData()
......@@ -50,4 +51,12 @@ export class InstagramController {
async addStoryMentions(@Body() addDto: AddStoryDataDto) {
return await this.instagramService.addStoryData(addDto)
}
@ApiBody({ type: AddStoryDataDto })
@ApiTags('Instagram')
@Post('add-website-sells')
async addWebSiteSells(@Body() addDto: AddWebsiteSellsDto) {
return await this.instagramService.addWebsiteSells(addDto)
}
}
......@@ -8,6 +8,7 @@ import { LikeSchema } from './models/like.schema';
import { LottoryResultSchema } from './models/LottoryResult.schema';
import { StoryMentionSchema } from './models/storyMention.schema'
import { UserSchema } from './models/user.schema';
import { WebsiteSellSchema } from './models/websiteSell.schema';
@Module({
imports: [
......@@ -29,6 +30,9 @@ import { UserSchema } from './models/user.schema';
MongooseModule.forFeature([
{ name: 'StoryMention', schema: StoryMentionSchema },
]),
MongooseModule.forFeature([
{ name: 'WebsiteSell', schema: WebsiteSellSchema },
]),
],
controllers: [InstagramController],
providers: [InstagramService]
......
......@@ -16,6 +16,8 @@ import * as path from "path"
import { StoryMentionDocument } from './models/storyMention.schema'
import { AddStoryDataDto } from './dto/add-story-data-dto'
import { Cron, CronExpression } from '@nestjs/schedule'
import { AddWebsiteSellsDto } from './dto/add-website-sell-dto'
import { WebsiteSellDocument } from './models/websiteSell.schema'
@Injectable()
export class InstagramService implements OnApplicationBootstrap {
......@@ -30,6 +32,8 @@ export class InstagramService implements OnApplicationBootstrap {
private commentModel: Model<CommentDocument>,
@InjectModel('StoryMention')
private storyMentionModel: Model<StoryMentionDocument>,
@InjectModel('WebsiteSell')
private websiteSellModel: Model<WebsiteSellDocument>,
) { }
......@@ -83,7 +87,7 @@ export class InstagramService implements OnApplicationBootstrap {
async getCommentsFromInstaLoader(username: string, password: string, post_short_code: string, profile: string) {
console.log("start getting comments");
let paths = path.resolve("src", "instagram", "instaloader-service", "getComments.py")
console.log(`${paths}`, `${username}`, `${password}`, `${post_short_code}`, `${profile}`);
const getCommentsProccess = spawn('python3', [`${paths}`, `${username}`, `${password}`, `${post_short_code}`, `${profile}`])
......@@ -151,6 +155,41 @@ export class InstagramService implements OnApplicationBootstrap {
count: userData.count
})
}
}
}
async addWebsiteSells(addDto: AddWebsiteSellsDto) {
for await (const userData of addDto.website_sells) {
let foundUser = await this.userModel.findOne({ username: userData.username })
if (foundUser) {
let foundData = await this.websiteSellModel.findOne({
user_id: new Types.ObjectId(foundUser._id)
})
if (!foundData) {
await this.websiteSellModel.create({
_id: new Types.ObjectId(),
user_id: foundUser._id,
value: userData.value
})
}
else {
await this.websiteSellModel.findOneAndUpdate({
user_id: new Types.ObjectId(foundUser._id),
}, { value: userData.value })
}
}
else {
let newUser = await this.userModel.create({
_id: new Types.ObjectId(),
username: userData.username,
is_follower: false
})
await this.websiteSellModel.create({
_id: new Types.ObjectId(),
user_id: newUser._id,
value: userData.value
})
}
}
}
......@@ -171,30 +210,30 @@ export class InstagramService implements OnApplicationBootstrap {
return user
}
@Cron(CronExpression.EVERY_3_HOURS)
async cronGetLikes() {
console.log('start cron ======> get daily Likes');
let username = "kakasrm"
let password = "kaka1374"
let postArray = ["CSlqcUEAZsU"]
let account = "azadi.gold"
for await (const post_short_code of postArray) {
await this.getLikesFromInstaLoader(username, password, post_short_code, account )
@Cron(CronExpression.EVERY_3_HOURS)
async cronGetLikes() {
console.log('start cron ======> get daily Likes');
let username = "kakasrm"
let password = "kaka1374"
let postArray = ["CSlqcUEAZsU"]
let account = "azadi.gold"
for await (const post_short_code of postArray) {
await this.getLikesFromInstaLoader(username, password, post_short_code, account)
}
}
}
@Cron(CronExpression.EVERY_3_HOURS)
async cronGetComments() {
console.log('start cron ======> calculate daily comments');
let username = "kakasrm"
let password = "kaka1374"
let postArray = ["CSlqcUEAZsU"]
let account = "azadi.gold"
for await (const post_short_code of postArray) {
await this.getCommentsFromInstaLoader(username, password, post_short_code, account )
@Cron(CronExpression.EVERY_3_HOURS)
async cronGetComments() {
console.log('start cron ======> calculate daily comments');
let username = "kakasrm"
let password = "kaka1374"
let postArray = ["CSlqcUEAZsU"]
let account = "azadi.gold"
for await (const post_short_code of postArray) {
await this.getCommentsFromInstaLoader(username, password, post_short_code, account)
}
}
}
}
......@@ -3,13 +3,13 @@ import pymongo
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
username = "soheib693"
password = "kaka1374"
# mongo_connection_string = "mongodb://instagram:wcD3B5sGw0yQ@185.231.180.248:27017/instagram-lottry?authSource=admin&authMechanism=SCRAM-SHA-256"
mongo_connection_string = "mongodb://localhost:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000"
database_name = "instagram-lottry"
post_short_code = str(sys.argv[3])
PROFILE = str(sys.argv[4])
post_short_code = "CT7aTDRjWQB"
PROFILE = "azadi.gold"
def __main__():
......
......@@ -3,17 +3,17 @@ import pymongo
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
username = "soheib693"
password = "kaka1374"
# mongo_connection_string = "mongodb://instagram:wcD3B5sGw0yQ@185.231.180.248:27017/instagram-lottry?authSource=admin&authMechanism=SCRAM-SHA-256"
mongo_connection_string = "mongodb://localhost:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000"
database_name = "instagram-lottry"
post_short_code = str(sys.argv[3])
PROFILE = str(sys.argv[4])
post_short_code = "CT7aTDRjWQB"
PROFILE = "azadi.gold"
def __main__():
print(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
# print(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
print("connecting to the instagram ....")
L = instaloader.Instaloader()
L.login(username, password)
......
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type WebsiteSellDocument = WebsiteSell & Document;
@Schema({ timestamps: true })
export class WebsiteSell {
@Prop()
_id: Types.ObjectId
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
@Prop()
value: number
}
export const WebsiteSellSchema =
SchemaFactory.createForClass(WebsiteSell)
\ No newline at end of file
......@@ -7,6 +7,7 @@ import { LottoryResultSchema } from 'src/instagram/models/LottoryResult.schema';
import { PostSchema } from 'src/instagram/models/post.schema'
import { StoryMentionSchema } from 'src/instagram/models/storyMention.schema'
import { UserSchema } from 'src/instagram/models/user.schema';
import { WebsiteSellSchema } from 'src/instagram/models/websiteSell.schema';
import { LotteryController } from './lottery.controller';
import { LotteryService } from './lottery.service';
import { ScoreService } from './score.service';
......@@ -34,6 +35,9 @@ import { ScoreService } from './score.service';
MongooseModule.forFeature([
{ name: 'StoryMention', schema: StoryMentionSchema },
]),
MongooseModule.forFeature([
{ name: 'WebsiteSell', schema: WebsiteSellSchema },
]),
],
controllers: [LotteryController],
providers: [LotteryService, ScoreService]
......
......@@ -10,6 +10,7 @@ import { ScoreService } from './score.service'
import { StoryMentionDocument } from 'src/instagram/models/storyMention.schema'
import { Cron, CronExpression } from '@nestjs/schedule'
import { PostDocument } from 'src/instagram/models/post.schema'
import { WebsiteSellDocument } from 'src/instagram/models/websiteSell.schema'
@Injectable()
export class LotteryService {
......@@ -27,6 +28,8 @@ export class LotteryService {
private lotteryResultModel: Model<LottoryResultDocument>,
@InjectModel('StoryMention')
private storyMentionModel: Model<StoryMentionDocument>,
@InjectModel('WebsiteSell')
private websiteSellModel: Model<WebsiteSellDocument>,
) { }
async getUserScore(username: string) {
......@@ -42,7 +45,8 @@ export class LotteryService {
"azadi.gold",
postArray,
)
const addToStoryScore = await this.scoreService.getTagsScore(username)
const addToStoryScore = await this.scoreService.getTagsScore(foundUser._id,)
const sellScore = await this.scoreService.getWebsiteSellsScore(foundUser._id)
let foundIndexes = await this.lotteryResultModel.find({ user_id: foundUser._id })
let chances = new Array<string>()
foundIndexes.forEach(result => {
......@@ -52,7 +56,9 @@ export class LotteryService {
likesScore,
commentScore,
addToStoryScore,
totalScore: likesScore + commentScore + addToStoryScore,
sellScore,
totalScore: likesScore + commentScore + addToStoryScore + sellScore,
lottry_chances: chances
}
}
......
......@@ -7,6 +7,7 @@ import { LikeDocument } from 'src/instagram/models/like.schema'
import { PostDocument } from 'src/instagram/models/post.schema'
import { StoryMentionDocument } from 'src/instagram/models/storyMention.schema'
import { UserDocument } from 'src/instagram/models/user.schema'
import { WebsiteSellDocument } from 'src/instagram/models/websiteSell.schema'
import addToStoryData from '../instagram/values/add-to-story-data'
......@@ -27,6 +28,8 @@ export class ScoreService {
private postModel: Model<PostDocument>,
@InjectModel('StoryMention')
private storyMentionModel: Model<StoryMentionDocument>,
@InjectModel('WebsiteSell')
private websiteSellModel: Model<WebsiteSellDocument>,
) { }
......@@ -102,19 +105,29 @@ export class ScoreService {
let addToStoryScore = 0
let foundUserInStory = await this.storyMentionModel.findOne({ user_id: new Types.ObjectId(user_id) })
if (foundUserInStory) {
addToStoryScore = (foundUserInStory.count*2)
addToStoryScore = (foundUserInStory.count * 2)
}
return addToStoryScore
}
async getWebsiteSellsScore(user_id: string) {
let sellScore = 0
let foundUserInSell = await this.websiteSellModel.findOne({ user_id: new Types.ObjectId(user_id) })
if (foundUserInSell) {
sellScore = (foundUserInSell.value / 1000000) * 30
}
return sellScore
}
async calculateUserScore(username: string, profileUsername: string, postArray: string[]) {
let likesScore = await this.getUserLikesScore(username, profileUsername, postArray)
let commentScore = await this.getUserCommentsScore(username, profileUsername, postArray)
let addToStoryScore = await this.getTagsScore(username)
return likesScore + commentScore + addToStoryScore
let sellScore = await this.getWebsiteSellsScore(username)
return likesScore + commentScore + addToStoryScore + sellScore
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment