Commit 78e8f3a4 authored by soheib's avatar soheib
Browse files

add service base

parent bfd98ea1
...@@ -8,9 +8,6 @@ export class AccountFollowers { ...@@ -8,9 +8,6 @@ export class AccountFollowers {
@Prop() @Prop()
_id: Types.ObjectId _id: Types.ObjectId
@Prop()
account_username: string
@Prop() @Prop()
username: string username: string
...@@ -18,10 +15,16 @@ export class AccountFollowers { ...@@ -18,10 +15,16 @@ export class AccountFollowers {
user_id: string user_id: string
@Prop() @Prop()
profile_pic: string full_name: string
@Prop() @Prop()
full_name: string bussines_username: string
@Prop({type:Object})
follower_obejct: Object
@Prop()
follow_date: number
} }
export const AccountFollowersSchema = SchemaFactory.createForClass(AccountFollowers) export const AccountFollowersSchema = SchemaFactory.createForClass(AccountFollowers)
......
import { Controller, Get } from '@nestjs/common'; import { Body, Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { GetUserScore } from './dto/get-user-score';
@Controller() @Controller()
export class AppController { export class AppController {
...@@ -17,8 +18,8 @@ export class AppController { ...@@ -17,8 +18,8 @@ export class AppController {
} }
@Get('clean-comments') @Post()
async cleanComments() { async getUserScore(@Body() getUserScoreDto: GetUserScore) {
return await this.appService.cleanUserComments(); return await this.appService.calculateUserScore(getUserScoreDto.username);
} }
} }
...@@ -3,19 +3,15 @@ import { MongooseModule } from '@nestjs/mongoose'; ...@@ -3,19 +3,15 @@ import { MongooseModule } from '@nestjs/mongoose';
import { AccountFollowersSchema } from './account.followers'; import { AccountFollowersSchema } from './account.followers';
import { AppController } from './app.controller'; import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { MentionSchema } from './cleanedcomment.schema';
import { CommentSchema } from './comment.schema'; import { CommentSchema } from './comment.schema';
import { RequestSchema } from './request.schema'; import { RequestSchema } from './request.schema';
import { UserSchema } from './user.schema';
@Module({ @Module({
imports: [ imports: [
MongooseModule.forRoot('mongodb://localhost/netware'), MongooseModule.forRoot('mongodb://localhost/netware'),
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]), MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]),
MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]), MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]),
MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]), MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]),
MongooseModule.forFeature([{ name: 'Mention', schema: MentionSchema }]),
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],
......
import { HttpException, Injectable } from '@nestjs/common'; import { HttpException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
import { IgApiClient } from 'instagram-private-api';
import { lowerFirst, sample } from 'lodash';
import { Model, Types } from 'mongoose'; import { Model, Types } from 'mongoose';
import { CommentDocument } from './comment.schema'; import { CommentDocument } from './comment.schema';
import { IFollower } from './interface/Ifollower'; import { IFollower } from './interface/Ifollower';
import { IncomingComment } from './interface/IncomingComment'; import { IncomingComment } from './interface/IncomingComment';
import { RequestDocument } from './request.schema'; import { RequestDocument } from './request.schema';
import { UserDocument } from './user.schema';
const Instagram = require('instagram-web-api') const Instagram = require('instagram-web-api')
const { username, password } = process.env const { username, password } = process.env
import * as _ from "lodash" import * as _ from "lodash"
import { MentionDocument } from './cleanedcomment.schema'; import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { AccountFollowers, AccountFollowersDocument } from './account.followers';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
@Injectable() @Injectable()
export class AppService { export class AppService {
constructor( constructor(
@InjectModel('User')
private userModel: Model<UserDocument>,
@InjectModel('Request') @InjectModel('Request')
private requestModel: Model<RequestDocument>, private requestModel: Model<RequestDocument>,
@InjectModel('Comment') @InjectModel('Comment')
private commentModel: Model<CommentDocument>, private commentModel: Model<CommentDocument>,
@InjectModel('AccountFollower') @InjectModel('AccountFollower')
private followerModel: Model<CommentDocument>, private followerModel: Model<AccountFollowersDocument>
@InjectModel('Mention')
private mentionModel: Model<MentionDocument>
) { } ) { }
async getFollowers(postShortCode: string = 'CRWNkkchs2x') { async getFollowers(postShortCode: string = 'CRWNkkchs2x') {
try { try {
const username = 'hesamhesam0202' const username = 'jangomangoss'
const password = "hesamh15352" const password = "kaka7701"
const client = new Instagram({ username, password }) const client = new Instagram({ username, password })
await client.login() await client.login()
console.log('user logged in...'); console.log('user logged in...');
...@@ -64,33 +60,28 @@ export class AppService { ...@@ -64,33 +60,28 @@ export class AppService {
console.log("nextCursor:", cursor) console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage) console.log("has a next page", hasNextPage)
console.log("object is: ", collectedFollower);
for await (const follower of collectedFollower.followers) { for await (const follower of collectedFollower.followers) {
let check = await this.followerModel.findOne({ let check = await this.followerModel.findOne({
$and: [ $and: [
{ username: follower.username, }, { username: follower.username, },
{ user_id: follower.user_id, }, { user_id: follower.user_id, },
{ full_name: follower.full_name, }
] ]
}) })
if (!check) { if (!check) {
await this.followerModel.create({ await this.followerModel.create({
_id: new Types.ObjectId(), _id: new Types.ObjectId(),
account_username: "azadi.gold",
username: follower.username, username: follower.username,
user_id: follower.user_id, user_id: follower.user_id,
full_name: follower.full_name, full_name: follower.full_name,
profile_pic: follower.profile_pic bussines_username: "azadi.gold",
follower_object: follower.follower_obejct,
follow_date: this.getFollowerDateOfFollow(follower.username)
}) })
followersCount += 1
} }
} }
console.log(collectedFollower.followers.length, "follower imported") console.log(followersCount, "follower imported")
followersCount += collectedFollower.followers.length console.log("================ end of this Request Proccess ==================");
console.log("total added", followersCount)
console.log("================ end of this iterration ==================");
} }
return { totalAdded: followersCount } return { totalAdded: followersCount }
...@@ -112,7 +103,7 @@ export class AppService { ...@@ -112,7 +103,7 @@ export class AppService {
let requestCount = 0 let requestCount = 0
let commentCount = 0 let commentCount = 0
let cursor: string = '' let cursor: string = ''
let reqList = await this.requestModel.find({ $and: [{ createdAt: -1 }, { type: "comment" }] }) let reqList = await this.requestModel.find({ type: "comment" }).sort({ createdAt: -1 })
console.log("Request History:", reqList.length) console.log("Request History:", reqList.length)
if (reqList.length != 0) { if (reqList.length != 0) {
...@@ -132,44 +123,34 @@ export class AppService { ...@@ -132,44 +123,34 @@ export class AppService {
cursor = collectedComments.cursor cursor = collectedComments.cursor
hasNextPage = collectedComments.hasNextPage hasNextPage = collectedComments.hasNextPage
for await (const comment of collectedComments.comments) { for await (const comment of collectedComments.comments) {
let check = await this.commentModel.findOne({ let check = await this.commentModel.findOne({
$and: [ comment_id: comment.comment_id
{ user_profile: comment.owner_id },
{ comment: comment.comment_value },
{ date: comment.date }
]
}) })
console.log("is this comment imported?", check);
if (!check) { if (!check) {
console.log('adding to database...'); await this.commentModel.create({
let res = await this.commentModel.create({
_id: new Types.ObjectId(), _id: new Types.ObjectId(),
user_profile: comment.owner_id, comment_id: comment.comment_id,
comment: comment.comment_value, owner_username: comment.owner_id,
owner_id: comment.owner_username,
text: comment.text,
comment_object: comment.commnet_object,
date: comment.date date: comment.date
}) })
console.log("imported comment", res); commentCount += 1
} }
} }
console.log("=================================="); console.log("================nex request info==================");
console.log("nextCursor:", cursor) console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage) console.log("has a next page", hasNextPage)
console.log(commentCount, "comment imported from pervios requests")
console.log("================ End of this Request Proccess ==================");
}
console.log(collectedComments.comments.length, "comment imported") return {
commentCount += collectedComments.comments.length status: "successfull",
console.log("total added", commentCount) totalAdded: commentCount
console.log("================ end of this iterration ==================");
} }
return { totalAdded: commentCount }
} }
catch (err) { catch (err) {
console.log(err) console.log(err)
...@@ -192,21 +173,20 @@ export class AppService { ...@@ -192,21 +173,20 @@ export class AppService {
try { try {
let comments: IncomingComment[] = new Array<IncomingComment>() let comments: IncomingComment[] = new Array<IncomingComment>()
let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: cursor }) let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: cursor })
await this.delay(_.random(20, 40)) await this.delay(_.random(2000, 10000))
console.log("=============incoming comments=============", incomingComments);
for (const comment of incomingComments.edges) { for (const comment of incomingComments.edges) {
console.log(comment); console.log("============data Object===============:", comment);
comments.push({ comments.push({
owner_id: comment.node.owner.username, comment_id: comment.node.id,
comment_value: comment.node.text, text: comment.node.text,
date: comment.node.created_at date: comment.node.created_at,
owner_id: comment.node.owner.id,
owner_username: comment.node.owner.username,
commnet_object: comment.node
}) })
console.log(`${comment.node.text} is pushed.`)
} }
console.log("incoming comment is : ", incomingComments);
return { return {
comments, comments,
cursor: incomingComments.page_info.end_cursor, cursor: incomingComments.page_info.end_cursor,
...@@ -223,8 +203,9 @@ export class AppService { ...@@ -223,8 +203,9 @@ export class AppService {
let Infollowers: IFollower[] = new Array<IFollower>() let Infollowers: IFollower[] = new Array<IFollower>()
const azadiGoldUser = await client.getUserByUsername({ username: 'azadi.gold' }) const azadiGoldUser = await client.getUserByUsername({ username: 'azadi.gold' })
const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor }) const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor })
await this.delay(1000) await this.delay(_.random(20000, 40000))
console.log("=============incoming followers=============", followers);
for (const user of followers.data) { for (const user of followers.data) {
...@@ -232,12 +213,9 @@ export class AppService { ...@@ -232,12 +213,9 @@ export class AppService {
user_id: user.id, user_id: user.id,
username: user.username, username: user.username,
full_name: user.full_name, full_name: user.full_name,
profile_pic: user.profile_pic_url follower_obejct: user
}) })
console.log(`${user.username} is pushed.`)
} }
return { return {
followers: Infollowers, followers: Infollowers,
cursor: followers.page_info.end_cursor, cursor: followers.page_info.end_cursor,
...@@ -257,51 +235,134 @@ export class AppService { ...@@ -257,51 +235,134 @@ export class AppService {
} }
async cleanUserComments() { async calculateUserScore(owner_username: string) {
let allComments = await this.commentModel.find() let foundUserComments = await this.commentModel.find({ owner_id: owner_username }).sort({createdAt: -1})
let isUserFollowPage = await this.checkUserFollowingStatus(owner_username)
let ownerUserFollowStatus: boolean
for await (const comment of allComments) { if (isUserFollowPage) {
ownerUserFollowStatus = true
}
else {
ownerUserFollowStatus = false
}
let rawComment = comment.comment let UseCleanComment = new CleanedComments()
let commentArray = rawComment.split(' ') UseCleanComment.mentions = new Array<MentionDocument>()
for await (const commentNode of commentArray) { UseCleanComment.owner_username = owner_username
if (commentNode.includes('@')) {
let foundMention = await this.mentionModel.findOne({ foundUserComments.forEach(comment => {
$and: [ let rawComment = comment.text
{ let commentTextArray = rawComment.split(' ')
owner_username: comment.user_profile, commentTextArray.forEach(commentSubStr => {
}, let check = false
{ if (commentSubStr.includes('@')) {
mentioned_username: commentNode, if (UseCleanComment.mentions.length !=0 ) {
UseCleanComment.mentions.forEach(mention => {
if (commentSubStr == mention.mentioned_username || commentSubStr == owner_username) {
check = true
} }
]
})
if (!foundMention) {
await this.mentionModel.create({
_id: new Types.ObjectId(),
owner_username: comment.user_profile,
mentioned_username: commentNode,
date: comment.date
}) })
} }
else {
if (commentSubStr == owner_username) {
check = true
}
}
if(check==false){
UseCleanComment.mentions.push({ mentioned_username: commentSubStr, date: comment.date })
}
} }
})
})
let allUserMentions = new Array<UserAllMention>()
for await (const mentionedUser of UseCleanComment.mentions) {
let newMentionData = new UserAllMention()
newMentionData.comment_status = new Array<CommentStatus>()
let foundAccount = await this.checkUserFollowingStatus(mentionedUser.mentioned_username.split('@')[1])
if (!foundAccount) {
newMentionData.mentioned_username = mentionedUser.mentioned_username,
newMentionData.page_follow_date = null,
newMentionData.comment_date = mentionedUser.date,
newMentionData.comment_status.push(CommentStatus.notFollower)
allUserMentions.push(newMentionData)
} }
else {
newMentionData.mentioned_username = foundAccount.username
newMentionData.mentioned_user_id = foundAccount.user_id
newMentionData.page_follow_date = foundAccount.follow_date
newMentionData.comment_date = mentionedUser.date
let checkUserFollowedPageBefore = await this.checkUserFollowedPageBefore(
newMentionData.comment_date, newMentionData.page_follow_date)
if (checkUserFollowedPageBefore == true) {
newMentionData.comment_status.push(CommentStatus.isAFollowerBefore)
}
let checkUserMentionedBefore = await this.checkUserMentionedBefore(
newMentionData.mentioned_username, newMentionData.comment_date)
if (checkUserMentionedBefore == true) {
newMentionData.comment_status.push(CommentStatus.isMentionedBefore)
}
if (checkUserFollowedPageBefore == false && checkUserMentionedBefore == false) {
newMentionData.comment_status.push(CommentStatus.isValid)
}
allUserMentions.push(newMentionData)
}
}
return {
mentions: allUserMentions
}
}
async checkUserMentionedBefore(username, comment_date) {
let foundCommentsWithThisMention = await this.commentModel.find({
text: new RegExp(`@${username}`)
})
let isValid = false
if (foundCommentsWithThisMention.length != 0) {
foundCommentsWithThisMention.forEach(comment => {
if (comment_date > comment.date) {
isValid = true
}
})
} }
return isValid
}
async checkUserFollowingStatus(username: string) {
let res = await this.followerModel.findOne({ username:username})
return res
} }
async checkUserFollowedPageBefore(comment_date: number, followed_date: number) {
if (comment_date < followed_date) {
return false
}
else {
return true
}
}
getFollowerDateOfFollow(username: string) {
let follower_objectResult: number
FollowerPrivateData.relationships_followers.forEach((follower_object) => {
if (follower_object.string_list_data[0]['value'].toString() === username.toString())
follower_objectResult = follower_object.string_list_data[0]['timestamp']
})
return follower_objectResult
}
} }
// let foundUser = await this.userModel.findOne({ username: foundComment.user_profile })
// if (!foundUser) {
// let addUser = await this.userModel.create({
// _id: new Types.ObjectId(),
// username: foundComment.user_profile,
// score: 0
// })
// user = addUser
// }
// else {
// user = foundUser
// }
\ No newline at end of file
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
export type MentionDocument = Mention & Document
@Schema()
export class Mention {
@Prop()
_id: Types.ObjectId
@Prop()
owner_username: string
@Prop()
mentioned_username : string
@Prop()
date: Date
}
export const MentionSchema = SchemaFactory.createForClass(Mention)
...@@ -3,19 +3,30 @@ import { Document, Types } from 'mongoose' ...@@ -3,19 +3,30 @@ import { Document, Types } from 'mongoose'
export type CommentDocument = Comment & Document export type CommentDocument = Comment & Document
@Schema() @Schema({timestamps:true})
export class Comment { export class Comment {
@Prop() @Prop()
_id: Types.ObjectId _id: Types.ObjectId
@Prop() @Prop()
comment: string comment_id: string
@Prop() @Prop()
user_profile : string text: string
@Prop() @Prop()
date: Date owner_username : string
@Prop()
owner_id : string
@Prop()
date: number
@Prop({type:Object})
comment_object : Object
} }
export const CommentSchema = SchemaFactory.createForClass(Comment) export const CommentSchema = SchemaFactory.createForClass(Comment)
......
export class GetUserScore {
username:string
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
export class CleanedComments{
owner_username: string
mentions : Array<MentionDocument>
}
export class MentionDocument{
mentioned_username: string
date: number
}
\ No newline at end of file
export interface IFollower{ export interface IFollower{
user_id: string user_id: string
username: string username: string
profile_pic: string
full_name: string full_name: string
follower_obejct: Object
follow_data?: number
} }
\ No newline at end of file
export interface IncomingComment { export interface IncomingComment {
comment_id: string
owner_username: string
owner_id: string owner_id: string
comment_value: string text: string
date: Date date: number
commnet_object : Object
} }
\ No newline at end of file
export class UserAllMention {
mentioned_username: string
mentioned_user_id: string
page_follow_date: number
comment_date: number
comment_status?: CommentStatus[]
}
export enum CommentStatus {
isMentionedBefore ="isMentionedBefore", //"this username was mentioned already before your comment",
isAFollowerBefore = "isAFollowerBefore", //"this username was followed page before your comment",
notFollower = "notFollower", //"this username didnt follow page yet",
isValid = "isValid" //"your comment is valid"
}
\ No newline at end of file
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { Document, Types } from 'mongoose'
export type UserDocument = User & Document
@Schema()
export class User {
@Prop()
_id: Types.ObjectId
@Prop()
username : string
@Prop({ type: [{ type: Types.ObjectId, ref: 'User' }] })
invitedUsers: Types.ObjectId[]
@Prop()
score : number
}
export const UserSchema = SchemaFactory.createForClass(User)
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