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

add service base

parent bfd98ea1
......@@ -8,9 +8,6 @@ export class AccountFollowers {
@Prop()
_id: Types.ObjectId
@Prop()
account_username: string
@Prop()
username: string
......@@ -18,10 +15,16 @@ export class AccountFollowers {
user_id: string
@Prop()
profile_pic: string
full_name: string
@Prop()
full_name: string
bussines_username: string
@Prop({type:Object})
follower_obejct: Object
@Prop()
follow_date: number
}
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 { GetUserScore } from './dto/get-user-score';
@Controller()
export class AppController {
......@@ -17,8 +18,8 @@ export class AppController {
}
@Get('clean-comments')
async cleanComments() {
return await this.appService.cleanUserComments();
@Post()
async getUserScore(@Body() getUserScoreDto: GetUserScore) {
return await this.appService.calculateUserScore(getUserScoreDto.username);
}
}
......@@ -3,19 +3,15 @@ import { MongooseModule } from '@nestjs/mongoose';
import { AccountFollowersSchema } from './account.followers';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { MentionSchema } from './cleanedcomment.schema';
import { CommentSchema } from './comment.schema';
import { RequestSchema } from './request.schema';
import { UserSchema } from './user.schema';
@Module({
imports: [
MongooseModule.forRoot('mongodb://localhost/netware'),
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }]),
MongooseModule.forFeature([{ name: 'Request', schema: RequestSchema }]),
MongooseModule.forFeature([{ name: 'Comment', schema: CommentSchema }]),
MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]),
MongooseModule.forFeature([{ name: 'Mention', schema: MentionSchema }]),
],
controllers: [AppController],
providers: [AppService],
......
import { HttpException, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { IgApiClient } from 'instagram-private-api';
import { lowerFirst, sample } from 'lodash';
import { Model, Types } from 'mongoose';
import { CommentDocument } from './comment.schema';
import { IFollower } from './interface/Ifollower';
import { IncomingComment } from './interface/IncomingComment';
import { RequestDocument } from './request.schema';
import { UserDocument } from './user.schema';
const Instagram = require('instagram-web-api')
const { username, password } = process.env
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()
export class AppService {
constructor(
@InjectModel('User')
private userModel: Model<UserDocument>,
@InjectModel('Request')
private requestModel: Model<RequestDocument>,
@InjectModel('Comment')
private commentModel: Model<CommentDocument>,
@InjectModel('AccountFollower')
private followerModel: Model<CommentDocument>,
@InjectModel('Mention')
private mentionModel: Model<MentionDocument>
private followerModel: Model<AccountFollowersDocument>
) { }
async getFollowers(postShortCode: string = 'CRWNkkchs2x') {
try {
const username = 'hesamhesam0202'
const password = "hesamh15352"
const username = 'jangomangoss'
const password = "kaka7701"
const client = new Instagram({ username, password })
await client.login()
console.log('user logged in...');
......@@ -64,33 +60,28 @@ export class AppService {
console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage)
console.log("object is: ", collectedFollower);
for await (const follower of collectedFollower.followers) {
let check = await this.followerModel.findOne({
$and: [
{ username: follower.username, },
{ user_id: follower.user_id, },
{ full_name: follower.full_name, }
]
})
if (!check) {
await this.followerModel.create({
_id: new Types.ObjectId(),
account_username: "azadi.gold",
username: follower.username,
user_id: follower.user_id,
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")
followersCount += collectedFollower.followers.length
console.log("total added", followersCount)
console.log("================ end of this iterration ==================");
console.log(followersCount, "follower imported")
console.log("================ end of this Request Proccess ==================");
}
return { totalAdded: followersCount }
......@@ -112,7 +103,7 @@ export class AppService {
let requestCount = 0
let commentCount = 0
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)
if (reqList.length != 0) {
......@@ -132,44 +123,34 @@ export class AppService {
cursor = collectedComments.cursor
hasNextPage = collectedComments.hasNextPage
for await (const comment of collectedComments.comments) {
let check = await this.commentModel.findOne({
$and: [
{ user_profile: comment.owner_id },
{ comment: comment.comment_value },
{ date: comment.date }
]
comment_id: comment.comment_id
})
console.log("is this comment imported?", check);
if (!check) {
console.log('adding to database...');
let res = await this.commentModel.create({
await this.commentModel.create({
_id: new Types.ObjectId(),
user_profile: comment.owner_id,
comment: comment.comment_value,
comment_id: comment.comment_id,
owner_username: comment.owner_id,
owner_id: comment.owner_username,
text: comment.text,
comment_object: comment.commnet_object,
date: comment.date
})
console.log("imported comment", res);
commentCount += 1
}
}
console.log("==================================");
console.log("================nex request info==================");
console.log("nextCursor:", cursor)
console.log("has a next page", hasNextPage)
console.log(collectedComments.comments.length, "comment imported")
commentCount += collectedComments.comments.length
console.log("total added", commentCount)
console.log("================ end of this iterration ==================");
console.log(commentCount, "comment imported from pervios requests")
console.log("================ End of this Request Proccess ==================");
}
return {
status: "successfull",
totalAdded: commentCount
}
return { totalAdded: commentCount }
}
catch (err) {
console.log(err)
......@@ -192,21 +173,20 @@ export class AppService {
try {
let comments: IncomingComment[] = new Array<IncomingComment>()
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) {
console.log(comment);
console.log("============data Object===============:", comment);
comments.push({
owner_id: comment.node.owner.username,
comment_value: comment.node.text,
date: comment.node.created_at
comment_id: comment.node.id,
text: comment.node.text,
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 {
comments,
cursor: incomingComments.page_info.end_cursor,
......@@ -223,8 +203,9 @@ export class AppService {
let Infollowers: IFollower[] = new Array<IFollower>()
const azadiGoldUser = await client.getUserByUsername({ username: 'azadi.gold' })
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) {
......@@ -232,12 +213,9 @@ export class AppService {
user_id: user.id,
username: user.username,
full_name: user.full_name,
profile_pic: user.profile_pic_url
follower_obejct: user
})
console.log(`${user.username} is pushed.`)
}
return {
followers: Infollowers,
cursor: followers.page_info.end_cursor,
......@@ -257,51 +235,134 @@ export class AppService {
}
async cleanUserComments() {
let allComments = await this.commentModel.find()
async calculateUserScore(owner_username: string) {
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 commentArray = rawComment.split(' ')
for await (const commentNode of commentArray) {
if (commentNode.includes('@')) {
let foundMention = await this.mentionModel.findOne({
$and: [
{
owner_username: comment.user_profile,
},
{
mentioned_username: commentNode,
let UseCleanComment = new CleanedComments()
UseCleanComment.mentions = new Array<MentionDocument>()
UseCleanComment.owner_username = owner_username
foundUserComments.forEach(comment => {
let rawComment = comment.text
let commentTextArray = rawComment.split(' ')
commentTextArray.forEach(commentSubStr => {
let check = false
if (commentSubStr.includes('@')) {
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'
export type CommentDocument = Comment & Document
@Schema()
@Schema({timestamps:true})
export class Comment {
@Prop()
_id: Types.ObjectId
@Prop()
comment: string
comment_id: string
@Prop()
user_profile : string
text: string
@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 class GetUserScore {
username:string
}
\ No newline at end of file
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
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{
user_id: string
username: string
profile_pic: string
full_name: string
follower_obejct: Object
follow_data?: number
}
\ No newline at end of file
export interface IncomingComment {
comment_id: string
owner_username: string
owner_id: string
comment_value: string
date: Date
text: string
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