Commit fbb24753 authored by shahriar's avatar shahriar
Browse files

fix bug

parent 24ac4281
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose';
export type LottoryResultDocument = LottoryResult & Document export type LottoryResultDocument = LottoryResult & Document;
@Schema({timestamps:true}) @Schema({ timestamps: true })
export class LottoryResult { export class LottoryResult {
@Prop() @Prop()
username: string username: string;
@Prop() @Prop()
index: number index: number;
} }
export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult) export const LottoryResultSchema = SchemaFactory.createForClass(LottoryResult);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose';
export type AccountFollowersDocument = AccountFollowers & Document export type AccountFollowersDocument = AccountFollowers & Document;
@Schema({timestamps:true}) @Schema({ timestamps: true })
export class AccountFollowers { export class AccountFollowers {
@Prop() @Prop()
_id: Types.ObjectId _id: Types.ObjectId;
@Prop() @Prop()
username: string username: string;
@Prop() @Prop()
user_id: string user_id: string;
@Prop() @Prop()
full_name: string full_name: string;
@Prop() @Prop()
bussines_username: string bussines_username: string;
@Prop({type:Object}) @Prop({ type: Object })
follower_obejct: Object follower_obejct: Object;
@Prop() @Prop()
follow_date: number follow_date: number;
} }
export const AccountFollowersSchema = SchemaFactory.createForClass(AccountFollowers) export const AccountFollowersSchema =
SchemaFactory.createForClass(AccountFollowers);
import { Body, Controller, Get, Param, Post } 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'; import { GetUserScore } from './dto/get-user-score';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) { }
@Get('get-comments') @Get('get-comments')
async getCommentsFromIG() { async getCommentsFromIG() {
return await this.appService.getComments(); return await this.appService.getComments();
} }
@Get('get-followers') @Get('get-followers')
async getFollowers() { async getFollowers() {
return await this.appService.getFollowers(); return await this.appService.getFollowers();
} }
@Post() @Post()
async getUserScore(@Body() getUserScoreDto: GetUserScore) { async getUserScore(@Body() getUserScoreDto: GetUserScore) {
return await this.appService.calculateUserScore(getUserScoreDto.username); return await this.appService.calculateUserScore(getUserScoreDto.username);
...@@ -23,7 +25,7 @@ export class AppController { ...@@ -23,7 +25,7 @@ export class AppController {
@Get('calculate-result') @Get('calculate-result')
async calculateResult() { async calculateResult() {
return await this.appService.getResults(); return await this.appService.getResults()
} }
@Get('get-results') @Get('get-results')
...@@ -31,18 +33,19 @@ export class AppController { ...@@ -31,18 +33,19 @@ export class AppController {
return await this.appService.getFinalResults(); return await this.appService.getFinalResults();
} }
@Get('search2/:id') @Post('search')
async getUserResults(@Param('id') id: string) { async getUserResults(@Body('username') username: string) {
return await this.appService.getUserResult(id); return await this.appService.getUserResult(username);
} }
@Get('shuffle') @Get('shuffle')
async shuffle() { async shuffle() {
return await this.appService.getShuffleData(); return await this.appService.getShuffleData()
} }
@Get('add-lottory-result') @Get('add-lottory-result')
async addResultDb() { async addResultDb() {
return await this.appService.addResultsToDB(); return await this.appService.addResultsToDB()
} }
} }
import { Module, OnApplicationBootstrap, } from '@nestjs/common'; import { Module, OnApplicationBootstrap } from '@nestjs/common';
import { MongooseModule } from '@nestjs/mongoose'; 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';
...@@ -10,15 +10,20 @@ import { ResultSchema } from './result.schema'; ...@@ -10,15 +10,20 @@ import { ResultSchema } from './result.schema';
@Module({ @Module({
imports: [ imports: [
MongooseModule.forRoot('mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256'), MongooseModule.forRoot(
'mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256',
),
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: 'Result', schema: ResultSchema }]), MongooseModule.forFeature([{ name: 'Result', schema: ResultSchema }]),
MongooseModule.forFeature([{ name: 'LottoryResult', schema: LottoryResultSchema }]), MongooseModule.forFeature([
MongooseModule.forFeature([{ name: 'AccountFollower', schema: AccountFollowersSchema }]), { name: 'LottoryResult', schema: LottoryResultSchema },
]),
MongooseModule.forFeature([
{ name: 'AccountFollower', schema: AccountFollowersSchema },
]),
], ],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],
}) })
export class AppModule {} export class AppModule {}
import { HttpException, Injectable, OnApplicationBootstrap } from '@nestjs/common'; import {
HttpException,
Injectable,
OnApplicationBootstrap,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose'; import { InjectModel } from '@nestjs/mongoose';
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';
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 FollowerPrivateData from './followers_data'; import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment'; import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { AccountFollowersDocument } from './account.followers'; import { AccountFollowersDocument } from './account.followers';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions'; import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
import { ResultDocument } from './result.schema'; import { ResultDocument } from './result.schema';
import { LottoryResultDocument } from './LottoryResult.schema'; import { LottoryResultDocument } from './LottoryResult.schema';
@Injectable() @Injectable()
export class AppService implements OnApplicationBootstrap { export class AppService implements OnApplicationBootstrap {
client: any client: any;
constructor( constructor(
@InjectModel('Request') @InjectModel('Request')
private requestModel: Model<RequestDocument>, private requestModel: Model<RequestDocument>,
...@@ -28,66 +32,72 @@ export class AppService implements OnApplicationBootstrap { ...@@ -28,66 +32,72 @@ export class AppService implements OnApplicationBootstrap {
@InjectModel('Result') @InjectModel('Result')
private resultModel: Model<ResultDocument>, private resultModel: Model<ResultDocument>,
@InjectModel('LottoryResult') @InjectModel('LottoryResult')
private lotoryResultModel: Model<LottoryResultDocument> private lotoryResultModel: Model<LottoryResultDocument>,
) { ) {}
}
async onApplicationBootstrap() { async onApplicationBootstrap() {
this.client = await this.login("sohe.ibs", "kaka1374") this.client = await this.login('sohe.ibs', 'kaka1374');
} }
private async login(username, password) { private async login(username, password) {
try { try {
const client = new Instagram({ username, password }) const client = new Instagram({ username, password });
await client.login() await client.login();
console.log(`user logged in. details :\n console.log(`user logged in. details :\n
username: ${username}, username: ${username},
password: ${password}, password: ${password},
`) `);
return client return client;
} } catch (err) {
catch (err) {
console.log(err); console.log(err);
} }
} }
async getFollowers(account_username: string = 'azadi.gold') { async getFollowers(account_username = 'azadi.gold') {
try { try {
let hasNextPage: boolean = true let hasNextPage = true;
let requestCount = 0 let requestCount = 0;
let followersCount = 0 let followersCount = 0;
let cursor: string = '' let cursor = '';
let reqList = await this.requestModel.find({ $and: [{ type: "follower" }, { account_username }] }).sort({ createdAt: -1 }) const reqList = await this.requestModel
.find({ $and: [{ type: 'follower' }, { account_username }] })
.sort({ createdAt: -1 });
console.log("Request History:", reqList.length) console.log('Request History:', reqList.length);
if (reqList.length != 0) { if (reqList.length != 0) {
// let nextCursor = await this.getFollowersNextCursor(client, reqList[0].cursor) // let nextCursor = await this.getFollowersNextCursor(client, reqList[0].cursor)
cursor = reqList[0].cursor cursor = reqList[0].cursor;
} }
while (hasNextPage) { while (hasNextPage) {
console.log("seted cursor", cursor) console.log('seted cursor', cursor);
console.log("sending request....") console.log('sending request....');
let collectedFollower = await this.sendFollowerRequest(this.client, account_username, '') const collectedFollower = await this.sendFollowerRequest(
console.log("request sended. request count:", requestCount); this.client,
requestCount++ account_username,
'',
);
cursor = collectedFollower.cursor console.log('request sended. request count:', requestCount);
await this.requestModel.create({ _id: new Types.ObjectId(), cursor: cursor, type: "follower", account_username }) requestCount++;
hasNextPage = collectedFollower.hasNextPage
cursor = collectedFollower.cursor;
await this.requestModel.create({
_id: new Types.ObjectId(),
cursor: cursor,
type: 'follower',
account_username,
});
hasNextPage = collectedFollower.hasNextPage;
for await (const follower of collectedFollower.followers) { for await (const follower of collectedFollower.followers) {
let check = await this.followerModel.findOne({ const check = await this.followerModel.findOne({
$and: [ $and: [
{ bussines_username: account_username }, { bussines_username: account_username },
{ username: follower.username, }, { username: follower.username },
{ user_id: follower.user_id, }, { user_id: follower.user_id },
] ],
}) });
if (!check) { if (!check) {
await this.followerModel.create({ await this.followerModel.create({
_id: new Types.ObjectId(), _id: new Types.ObjectId(),
...@@ -96,39 +106,38 @@ export class AppService implements OnApplicationBootstrap { ...@@ -96,39 +106,38 @@ export class AppService implements OnApplicationBootstrap {
full_name: follower.full_name, full_name: follower.full_name,
bussines_username: account_username, bussines_username: account_username,
follower_object: follower.follower_obejct, follower_object: follower.follower_obejct,
follow_date: this.getFollowerDateOfFollow(follower.username) follow_date: this.getFollowerDateOfFollow(follower.username),
}) });
followersCount += 1 followersCount += 1;
} } else {
else { return 'already updated';
return "already updated"
} }
} }
console.log("================next request info=================="); console.log('================next 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(followersCount, "followers imported from pervios requests") console.log(followersCount, 'followers imported from pervios requests');
console.log("================ end of this Request Proccess =================="); console.log(
'================ end of this Request Proccess ==================',
);
} }
return { return {
status: "successfull", status: 'successfull',
totalAdded: followersCount totalAdded: followersCount,
} };
} } catch (err) {
catch (err) { console.log(err);
console.log(err) throw new HttpException(err.message, 500);
throw new HttpException(err.message, 500)
} }
} }
async getComments(postShortCode: string = 'CRWNkkchs2x') { async getComments(postShortCode = 'CRWNkkchs2x') {
try { try {
let hasNextPage: boolean = true let hasNextPage = true;
let requestCount = 0 let requestCount = 0;
let commentCount = 0 let commentCount = 0;
let cursor: string = '' let cursor = '';
// let reqList = await this.requestModel.find({ $and: [{ type: "comment" }, { post_short_code: postShortCode }] }).sort({ createdAt: -1 }) // let reqList = await this.requestModel.find({ $and: [{ type: "comment" }, { post_short_code: postShortCode }] }).sort({ createdAt: -1 })
// console.log("Request History:", reqList.length) // console.log("Request History:", reqList.length)
...@@ -139,22 +148,30 @@ export class AppService implements OnApplicationBootstrap { ...@@ -139,22 +148,30 @@ export class AppService implements OnApplicationBootstrap {
// cursor = reqList[0].cursor // cursor = reqList[0].cursor
// } // }
while (hasNextPage) { while (hasNextPage) {
// console.log("seted cursor", cursor) // console.log("seted cursor", cursor)
console.log("sending request....") console.log('sending request....');
let collectedComments = await this.sendCommentRequest(this.client, postShortCode, '') const collectedComments = await this.sendCommentRequest(
console.log("request sended. request count:", requestCount); this.client,
requestCount++ postShortCode,
'',
cursor = collectedComments.cursor );
await this.requestModel.create({ _id: new Types.ObjectId(), cursor: cursor, type: "comment", post_short_code: postShortCode }) console.log('request sended. request count:', requestCount);
hasNextPage = collectedComments.hasNextPage requestCount++;
cursor = collectedComments.cursor;
await this.requestModel.create({
_id: new Types.ObjectId(),
cursor: cursor,
type: 'comment',
post_short_code: postShortCode,
});
hasNextPage = collectedComments.hasNextPage;
for await (const comment of collectedComments.comments) { for await (const comment of collectedComments.comments) {
let check = await this.commentModel.findOne({ const check = await this.commentModel.findOne({
comment_id: comment.comment_id comment_id: comment.comment_id,
}) });
if (!check) { if (!check) {
await this.commentModel.create({ await this.commentModel.create({
_id: new Types.ObjectId(), _id: new Types.ObjectId(),
...@@ -163,64 +180,87 @@ export class AppService implements OnApplicationBootstrap { ...@@ -163,64 +180,87 @@ export class AppService implements OnApplicationBootstrap {
owner_id: comment.owner_id, owner_id: comment.owner_id,
text: comment.text, text: comment.text,
comment_object: comment.commnet_object, comment_object: comment.commnet_object,
date: comment.date date: comment.date,
}) });
commentCount += 1 commentCount += 1;
} } else {
else { return 'already updated';
return "already updated"
} }
} }
console.log("================nex request info=================="); 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(commentCount, 'comment imported from pervios requests');
console.log("================ End of this Request Proccess =================="); console.log(
'================ End of this Request Proccess ==================',
);
} }
return { return {
status: "successfull", status: 'successfull',
totalAdded: commentCount totalAdded: commentCount,
} };
} } catch (err) {
catch (err) { console.log(err);
console.log(err) throw new HttpException(err.message, 500);
throw new HttpException(err.message, 500)
} }
} }
async getCommentsNextCursor(client, cursor: string, postShortCode: string) { async getCommentsNextCursor(client, cursor: string, postShortCode: string) {
console.log(`----------------------parameters------------------------------------------\n`, cursor, postShortCode); console.log(
let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: cursor }) `----------------------parameters------------------------------------------\n`,
console.log(`----------------------incomingComments------------------------------------------\n`, incomingComments); cursor,
postShortCode,
return incomingComments.page_info.end_cursor );
const incomingComments = await client.getMediaComments({
shortcode: postShortCode,
first: '49',
after: cursor,
});
console.log(
`----------------------incomingComments------------------------------------------\n`,
incomingComments,
);
return incomingComments.page_info.end_cursor;
} }
async getFollowersNextCursor(client, cursor: string) { async getFollowersNextCursor(client, cursor: string) {
const azadiGoldUser = await client.getUserByUsername({ username: 'azadi.gold' }) const azadiGoldUser = await client.getUserByUsername({
const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor }) username: 'azadi.gold',
return followers.page_info.end_cursor });
const followers = await client.getFollowers({
userId: azadiGoldUser.id,
after: cursor,
});
return followers.page_info.end_cursor;
} }
async sendCommentRequest(client, postShortCode, cursor) { async sendCommentRequest(client, postShortCode, cursor) {
try { try {
let comments: IncomingComment[] = new Array<IncomingComment>() const comments: IncomingComment[] = new Array<IncomingComment>();
let incomingComments = await client.getMediaComments({ shortcode: postShortCode, first: "49", after: '' }) const incomingComments = await client.getMediaComments({
shortcode: postShortCode,
first: '49',
after: '',
});
console.log("=============incoming comments=============", incomingComments); console.log(
'=============incoming comments=============',
incomingComments,
);
for (const comment of incomingComments.edges) { for (const comment of incomingComments.edges) {
console.log("============data Object===============:", comment); console.log('============data Object===============:', comment);
comments.push({ comments.push({
comment_id: comment.node.id, comment_id: comment.node.id,
text: comment.node.text, text: comment.node.text,
date: comment.node.created_at, date: comment.node.created_at,
owner_id: comment.node.owner.id, owner_id: comment.node.owner.id,
owner_username: comment.node.owner.username, owner_username: comment.node.owner.username,
commnet_object: comment.node commnet_object: comment.node,
}) });
} }
let pointer = incomingComments.page_info.end_cursor; let pointer = incomingComments.page_info.end_cursor;
...@@ -231,37 +271,37 @@ export class AppService implements OnApplicationBootstrap { ...@@ -231,37 +271,37 @@ export class AppService implements OnApplicationBootstrap {
} catch (e) { } catch (e) {
console.log(`Pointer is not array!, dont need to be converted!`); console.log(`Pointer is not array!, dont need to be converted!`);
} }
await this.delay(_.random(2000, 10000)) await this.delay(_.random(2000, 10000));
return { return {
comments, comments,
cursor: '', cursor: '',
hasNextPage: incomingComments.page_info.has_next_page hasNextPage: incomingComments.page_info.has_next_page,
} };
} } catch (err) {
catch (err) { console.log(err);
console.log(err) throw new HttpException(err.message, 500);
throw new HttpException(err.message, 500)
} }
} }
async sendFollowerRequest(client, username, cursor) { async sendFollowerRequest(client, username, cursor) {
try { try {
let Infollowers: IFollower[] = new Array<IFollower>() const Infollowers: IFollower[] = new Array<IFollower>();
await this.delay(_.random(2000, 10000)) await this.delay(_.random(2000, 10000));
const azadiGoldUser = await client.getUserByUsername({ username }) const azadiGoldUser = await client.getUserByUsername({ username });
const followers = await client.getFollowers({ userId: azadiGoldUser.id, after: cursor }) const followers = await client.getFollowers({
userId: azadiGoldUser.id,
after: cursor,
});
console.log("=============incoming followers=============", followers); console.log('=============incoming followers=============', followers);
for (const user of followers.data) { for (const user of followers.data) {
Infollowers.push({ Infollowers.push({
user_id: user.id, user_id: user.id,
username: user.username, username: user.username,
full_name: user.full_name, full_name: user.full_name,
follower_obejct: user follower_obejct: user,
}) });
} }
let pointer = followers.page_info.end_cursor; let pointer = followers.page_info.end_cursor;
// this will try to convert array to json stringify // this will try to convert array to json stringify
...@@ -274,146 +314,153 @@ export class AppService implements OnApplicationBootstrap { ...@@ -274,146 +314,153 @@ export class AppService implements OnApplicationBootstrap {
return { return {
followers: Infollowers, followers: Infollowers,
cursor: '', cursor: '',
hasNextPage: followers.page_info.has_next_page hasNextPage: followers.page_info.has_next_page,
} };
} } catch (err) {
catch (err) { console.log(err);
console.log(err) throw new HttpException(err.message, 500);
throw new HttpException(err.message, 500)
} }
} }
async delay(ms) { async delay(ms) {
// return await for better async stack trace support in case of errors. // return await for better async stack trace support in case of errors.
console.log('delay time:', ms); console.log('delay time:', ms);
return await new Promise(resolve => setTimeout(resolve, ms)); return await new Promise((resolve) => setTimeout(resolve, ms));
} }
async calculateUserScore(owner_username: string) { async calculateUserScore(owner_username: string) {
let foundUserComments = await this.commentModel.find({ owner_username }).sort({ createdAt: -1 }) const foundUserComments = await this.commentModel
let isUserFollowPage = await this.checkUserFollowingStatus(owner_username) .find({ owner_username })
let ownerUserFollowStatus: boolean .sort({ createdAt: -1 });
const isUserFollowPage = await this.checkUserFollowingStatus(
owner_username,
);
let ownerUserFollowStatus: boolean;
if (isUserFollowPage) { if (isUserFollowPage) {
ownerUserFollowStatus = true ownerUserFollowStatus = true;
} } else {
else { ownerUserFollowStatus = false;
ownerUserFollowStatus = false
} }
let UseCleanComment = new CleanedComments() const UseCleanComment = new CleanedComments();
UseCleanComment.mentions = new Array<MentionDocument>() UseCleanComment.mentions = new Array<MentionDocument>();
UseCleanComment.owner_username = owner_username UseCleanComment.owner_username = owner_username;
foundUserComments.forEach(comment => { foundUserComments.forEach((comment) => {
let rawComment = comment.text const rawComment = comment.text;
let commentTextArray = rawComment.split(' ') const commentTextArray = rawComment.split(' ');
commentTextArray.forEach(commentSubStr => { commentTextArray.forEach((commentSubStr) => {
let check = false let check = false;
if (commentSubStr.includes('@')) { if (commentSubStr.includes('@')) {
if (UseCleanComment.mentions.length != 0) { if (UseCleanComment.mentions.length != 0) {
UseCleanComment.mentions.forEach(mention => { UseCleanComment.mentions.forEach((mention) => {
if (commentSubStr == mention.mentioned_username || commentSubStr == owner_username) { if (
check = true commentSubStr == mention.mentioned_username ||
commentSubStr == owner_username
) {
check = true;
} }
}) });
} } else {
else {
if (commentSubStr == owner_username) { if (commentSubStr == owner_username) {
check = true check = true;
} }
} }
if (check == false) { if (check == false) {
UseCleanComment.mentions.push({ mentioned_username: commentSubStr, date: comment.date }) UseCleanComment.mentions.push({
mentioned_username: commentSubStr,
date: comment.date,
});
} }
} }
}) });
}) });
const allUserMentions = new Array<UserAllMention>();
let allUserMentions = new Array<UserAllMention>()
for await (const mentionedUser of UseCleanComment.mentions) { for await (const mentionedUser of UseCleanComment.mentions) {
const newMentionData = new UserAllMention();
newMentionData.comment_status = new Array<CommentStatus>();
let newMentionData = new UserAllMention() const foundAccount = await this.checkUserFollowingStatus(
newMentionData.comment_status = new Array<CommentStatus>() mentionedUser.mentioned_username.split('@')[1],
);
let foundAccount = await this.checkUserFollowingStatus(mentionedUser.mentioned_username.split('@')[1])
if (!foundAccount) { if (!foundAccount) {
newMentionData.mentioned_username = mentionedUser.mentioned_username, (newMentionData.mentioned_username = mentionedUser.mentioned_username),
newMentionData.page_follow_date = null, (newMentionData.page_follow_date = null),
newMentionData.comment_date = mentionedUser.date, (newMentionData.comment_date = mentionedUser.date),
newMentionData.comment_status.push(CommentStatus.notFollower) 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
allUserMentions.push(newMentionData);
let checkUserFollowedPageBefore = await this.checkUserFollowedPageBefore( } else {
newMentionData.comment_date, newMentionData.page_follow_date) newMentionData.mentioned_username = foundAccount.username;
newMentionData.mentioned_user_id = foundAccount.user_id;
newMentionData.page_follow_date = foundAccount.follow_date;
newMentionData.comment_date = mentionedUser.date;
const checkUserFollowedPageBefore =
await this.checkUserFollowedPageBefore(
newMentionData.comment_date,
newMentionData.page_follow_date,
);
if (checkUserFollowedPageBefore == true) { if (checkUserFollowedPageBefore == true) {
newMentionData.comment_status.push(CommentStatus.isAFollowerBefore) newMentionData.comment_status.push(CommentStatus.isAFollowerBefore);
} }
const checkUserMentionedBefore = await this.checkUserMentionedBefore(
let checkUserMentionedBefore = await this.checkUserMentionedBefore( newMentionData.mentioned_username,
newMentionData.mentioned_username, newMentionData.comment_date) newMentionData.comment_date,
);
if (checkUserMentionedBefore == true) { if (checkUserMentionedBefore == true) {
newMentionData.comment_status.push(CommentStatus.isMentionedBefore) newMentionData.comment_status.push(CommentStatus.isMentionedBefore);
} }
if (checkUserFollowedPageBefore == false && checkUserMentionedBefore == false) { if (
newMentionData.comment_status.push(CommentStatus.isValid) checkUserFollowedPageBefore == false &&
checkUserMentionedBefore == false
) {
newMentionData.comment_status.push(CommentStatus.isValid);
} }
allUserMentions.push(newMentionData) allUserMentions.push(newMentionData);
} }
} }
return { return {
mentions: allUserMentions mentions: allUserMentions,
} };
} }
async getResults() { async getResults() {
let foundUsernames = await this.commentModel.distinct('owner_username') const foundUsernames = await this.commentModel.distinct('owner_username');
console.log("total users for ranking:", foundUsernames.length); console.log('total users for ranking:', foundUsernames.length);
for await (const username of foundUsernames) { for await (const username of foundUsernames) {
const mentions = await this.calculateUserScore(username);
let mentions = await this.calculateUserScore(username) let valid_mentions = 0;
let valid_mentions = 0 let invalid_mentions = 0;
let invalid_mentions = 0 let pending_mentions = 0;
let pending_mentions = 0 const valid_users = new Array<string>();
let valid_users = new Array<string>() const inValid_users = new Array<string>();
let inValid_users = new Array<string>() const pending_users = new Array<string>();
let pending_users = new Array<string>()
mentions.mentions.forEach((mention) => {
mentions.mentions.forEach(mention => {
if (mention.comment_status.includes(CommentStatus.isValid)) { if (mention.comment_status.includes(CommentStatus.isValid)) {
valid_mentions++ valid_mentions++;
valid_users.push(mention.mentioned_username) valid_users.push(mention.mentioned_username);
} } else if (
else if (mention.comment_status.includes(CommentStatus.isMentionedBefore) mention.comment_status.includes(CommentStatus.isMentionedBefore) ||
|| mention.comment_status.includes(CommentStatus.isAFollowerBefore)) { mention.comment_status.includes(CommentStatus.isAFollowerBefore)
invalid_mentions++ ) {
inValid_users.push(mention.mentioned_username) invalid_mentions++;
inValid_users.push(mention.mentioned_username);
} else if (mention.comment_status.includes(CommentStatus.notFollower)) {
pending_mentions++;
pending_users.push(mention.mentioned_username);
} }
else if (mention.comment_status.includes(CommentStatus.notFollower)) { });
pending_mentions++
pending_users.push(mention.mentioned_username)
}
})
await this.delay(_.random(500, 1000)) await this.delay(_.random(500, 1000));
let foundUser = await this.resultModel.findOne({ username: username }) const foundUser = await this.resultModel.findOne({ username: username });
if (!foundUser) { if (!foundUser) {
await this.resultModel.create({ await this.resultModel.create({
username: username, username: username,
...@@ -421,212 +468,211 @@ export class AppService implements OnApplicationBootstrap { ...@@ -421,212 +468,211 @@ export class AppService implements OnApplicationBootstrap {
invalid_mentions, invalid_mentions,
pending_mentions, pending_mentions,
score: valid_mentions + 1, score: valid_mentions + 1,
valid_users: valid_users valid_users: valid_users,
, inValid_users: inValid_users, inValid_users: inValid_users,
pending_users: pending_users pending_users: pending_users,
}) });
} else { } else {
await this.resultModel.updateOne({ _id: foundUser._id }, { await this.resultModel.updateOne(
username: username, { _id: foundUser._id },
valid_mentions, {
invalid_mentions, username: username,
pending_mentions, valid_mentions,
score: valid_mentions + 1, invalid_mentions,
valid_users: valid_users pending_mentions,
, inValid_users: inValid_users, score: valid_mentions + 1,
pending_users: pending_users valid_users: valid_users,
}) inValid_users: inValid_users,
pending_users: pending_users,
},
);
} }
} }
return "records updated successfully" return 'records updated successfully';
} }
async checkUserMentionedBefore(username, comment_date) { async checkUserMentionedBefore(username, comment_date) {
let foundCommentsWithThisMention = await this.commentModel.find({ const foundCommentsWithThisMention = await this.commentModel.find({
text: new RegExp(`@${username}`) text: new RegExp(`@${username}`),
}) });
let isValid = false let isValid = false;
if (foundCommentsWithThisMention.length != 0) { if (foundCommentsWithThisMention.length != 0) {
foundCommentsWithThisMention.forEach(comment => { foundCommentsWithThisMention.forEach((comment) => {
if (comment_date > comment.date) { if (comment_date > comment.date) {
isValid = true isValid = true;
} }
}) });
} }
return isValid return isValid;
} }
async checkUserFollowingStatus(username: string) { async checkUserFollowingStatus(username: string) {
let res = await this.followerModel.findOne({ username: username }) const res = await this.followerModel.findOne({ username: username });
return res return res;
} }
async checkUserFollowedPageBefore(comment_date: number, followed_date: number) { async checkUserFollowedPageBefore(
comment_date: number,
followed_date: number,
) {
if (comment_date < followed_date) { if (comment_date < followed_date) {
return false return false;
} } else {
else { return true;
return true
} }
} }
getFollowerDateOfFollow(username: string) { getFollowerDateOfFollow(username: string) {
let follower_objectResult: number let follower_objectResult: number;
FollowerPrivateData.relationships_followers.forEach((follower_object) => { FollowerPrivateData.relationships_followers.forEach((follower_object) => {
if (follower_object.string_list_data[0]['value'].toString() === username.toString()) if (
follower_objectResult = follower_object.string_list_data[0]['timestamp'] follower_object.string_list_data[0]['value'].toString() ===
username.toString()
)
follower_objectResult =
follower_object.string_list_data[0]['timestamp'];
else { else {
follower_objectResult = Date.now() follower_objectResult = Date.now();
} }
}) });
return follower_objectResult return follower_objectResult;
} }
async getFinalResults() { async getFinalResults() {
const results = await this.resultModel.find().sort({ score: -1 });
let results = await this.resultModel.find().sort({ score: -1 }) const last_update = await this.resultModel.find().sort({ updatedAt: -1 });
let last_update = await this.resultModel.find().sort({ updatedAt: -1 }) const last_create = await this.resultModel.find().sort({ createdAt: -1 });
let last_create = await this.resultModel.find().sort({ createdAt: -1 }) let date: number;
let date: number console.log(last_update[0]['updatedAt']);
console.log(last_update[0]['updatedAt']) console.log(last_create[0]['createdAt']);
console.log(last_create[0]['createdAt'])
if (last_update[0]['updatedAt'] >= last_create[0]['createdAt']) { if (last_update[0]['updatedAt'] >= last_create[0]['createdAt']) {
date = last_update[0]['updatedAt'] date = last_update[0]['updatedAt'];
} } else {
else { date = last_create[0]['createdAt'];
date = last_create[0]['createdAt']
} }
let finalResult = new Array<ResultResponse>() const finalResult = new Array<ResultResponse>();
for await (const userRes of results) { for await (const userRes of results) {
let response: ResultResponse = new ResultResponse() const response: ResultResponse = new ResultResponse();
response.users = new Array<any>() response.users = new Array<any>();
userRes.pending_users.forEach(user => { userRes.pending_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.notFollower }) response.users.push({
}) userId: user,
userRes.inValid_users.forEach(user => { status: CommentStatus.notFollower,
response.users.push({ userId: user, status: CommentStatus.inValid }) });
}) });
userRes.valid_users.forEach(user => { userRes.inValid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.isValid }) response.users.push({ userId: user, status: CommentStatus.inValid });
}) });
userRes.valid_users.forEach((user) => {
response.username = userRes.username, response.users.push({ userId: user, status: CommentStatus.isValid });
response.valid_mentions = userRes.valid_mentions, });
response.invalid_mentions = userRes.invalid_mentions,
response.pending_mentions = userRes.pending_mentions, (response.username = userRes.username),
response.score = userRes.score (response.valid_mentions = userRes.valid_mentions),
(response.invalid_mentions = userRes.invalid_mentions),
finalResult.push(response) (response.pending_mentions = userRes.pending_mentions),
(response.score = userRes.score);
finalResult.push(response);
} }
return { return {
finalResult, finalResult,
last_update: date last_update: date,
} };
} }
async getUserResult(username: string) { async getUserResult(username: string) {
let userRes = await this.resultModel.findOne({ username }) const userRes = await this.resultModel.findOne({ username });
let userIndexs = await this.lotoryResultModel.find({username}) const userIndexs = await this.lotoryResultModel.find({ username });
let response: ResultResponse = new ResultResponse() const response: ResultResponse = new ResultResponse();
response.users = new Array<any>() response.users = new Array<any>();
response.lottory_chances_codes = new Array<string>() response.lottory_chances_codes = new Array<string>();
userRes.pending_users.forEach(user => { userRes.pending_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.notFollower }) response.users.push({ userId: user, status: CommentStatus.notFollower });
}) });
userRes.inValid_users.forEach(user => { userRes.inValid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.inValid }) response.users.push({ userId: user, status: CommentStatus.inValid });
}) });
userRes.valid_users.forEach(user => { userRes.valid_users.forEach((user) => {
response.users.push({ userId: user, status: CommentStatus.isValid }) response.users.push({ userId: user, status: CommentStatus.isValid });
}) });
response.username = userRes.username, (response.username = userRes.username),
response.valid_mentions = userRes.valid_mentions, (response.valid_mentions = userRes.valid_mentions),
response.invalid_mentions = userRes.invalid_mentions, (response.invalid_mentions = userRes.invalid_mentions),
response.pending_mentions = userRes.pending_mentions, (response.pending_mentions = userRes.pending_mentions),
response.score = userRes.score (response.score = userRes.score);
userIndexs.forEach(index =>{ userIndexs.forEach((index) => {
response.lottory_chances_codes.push(index.index.toString()) response.lottory_chances_codes.push(index.index.toString());
}) });
return { return {
response response,
} };
} }
shuffle(array) { shuffle(array) {
var currentIndex = array.length, randomIndex; let currentIndex = array.length,
randomIndex;
// While there remain elements to shuffle... // While there remain elements to shuffle...
while (0 !== currentIndex) { while (0 !== currentIndex) {
// Pick a remaining element... // Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex); randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--; currentIndex--;
// And swap it with the current element. // And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [ [array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]]; array[randomIndex],
array[currentIndex],
];
} }
return array; return array;
} }
async getShuffleData() { async getShuffleData() {
let comptitionArray = new Array<string>() const comptitionArray = new Array<string>();
let foundUsernames = await this.resultModel.find() const foundUsernames = await this.resultModel.find();
console.log(foundUsernames); console.log(foundUsernames);
let score = 0 let score = 0;
for await (const user of foundUsernames) { for await (const user of foundUsernames) {
score += user.score score += user.score;
for (let index = 0; index < user.score; index++) { for (let index = 0; index < user.score; index++) {
comptitionArray.push(user.username) comptitionArray.push(user.username);
} }
} }
let res = this.shuffle(comptitionArray) const res = this.shuffle(comptitionArray);
console.log("score is",score); console.log('score is', score);
return res
}
return res;
}
async addResultsToDB(){ async addResultsToDB() {
await this.lotoryResultModel.deleteMany() await this.lotoryResultModel.deleteMany();
let comptitionArray = new Array<any>() const comptitionArray = new Array<any>();
let foundUsernames = await this.resultModel.find().sort({ score: -1 }) const foundUsernames = await this.resultModel.find().sort({ score: -1 });
console.log(foundUsernames); console.log(foundUsernames);
let index = 1 let index = 1;
for await (const user of foundUsernames) { for await (const user of foundUsernames) {
for (let u = 0; u < user.score; u++) { for (let u = 0; u < user.score; u++) {
comptitionArray.push({username: user.username, index}) comptitionArray.push({ username: user.username, index });
index++ index++;
} }
} }
await this.lotoryResultModel.insertMany(comptitionArray) await this.lotoryResultModel.insertMany(comptitionArray);
return "successfull" return 'successfull';
} }
} }
export class ResultResponse { export class ResultResponse {
username: string username: string;
valid_mentions: number valid_mentions: number;
invalid_mentions: number invalid_mentions: number;
pending_mentions: number pending_mentions: number;
score: number score: number;
users?: Array<any> users?: Array<any>;
lottory_chances_codes :Array<string> lottory_chances_codes: Array<string>;
} }
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose';
export type CommentDocument = Comment & Document export type CommentDocument = Comment & Document;
@Schema({timestamps:true}) @Schema({ timestamps: true })
export class Comment { export class Comment {
@Prop() @Prop()
_id: Types.ObjectId _id: Types.ObjectId;
@Prop() @Prop()
comment_id: string comment_id: string;
@Prop() @Prop()
text: string text: string;
@Prop() @Prop()
owner_username : string owner_username: string;
@Prop() @Prop()
owner_id : string owner_id: string;
@Prop() @Prop()
date: number date: number;
@Prop({type:Object})
comment_object : Object
@Prop({ type: Object })
comment_object: Object;
} }
export const CommentSchema = SchemaFactory.createForClass(Comment) export const CommentSchema = SchemaFactory.createForClass(Comment);
export class GetUserScore { export class GetUserScore {
username: string;
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.
export class CleanedComments{ export class CleanedComments {
owner_username: string owner_username: string;
mentions : Array<MentionDocument> mentions: Array<MentionDocument>;
} }
export class MentionDocument{ export class MentionDocument {
mentioned_username: string mentioned_username: string;
date: number date: number;
} }
\ No newline at end of file
export interface IFollower{ export interface IFollower {
user_id: string user_id: string;
username: string username: string;
full_name: string full_name: string;
follower_obejct: Object follower_obejct: Object;
follow_data?: number follow_data?: number;
} }
\ No newline at end of file
export interface IncomingComment { export interface IncomingComment {
comment_id: string comment_id: string;
owner_username: string owner_username: string;
owner_id: string owner_id: string;
text: string text: string;
date: number date: number;
commnet_object : Object commnet_object: Object;
} }
\ No newline at end of file
export class UserAllMention { export class UserAllMention {
mentioned_username: string mentioned_username: string;
mentioned_user_id: string mentioned_user_id: string;
page_follow_date: number page_follow_date: number;
comment_date: number comment_date: number;
comment_status?: CommentStatus[] comment_status?: CommentStatus[];
} }
export enum CommentStatus { export enum CommentStatus {
isMentionedBefore ="isMentionedBefore", //"this username was mentioned already before your comment", isMentionedBefore = 'isMentionedBefore', //"this username was mentioned already before your comment",
isAFollowerBefore = "isAFollowerBefore", //"this username was followed page before your comment", isAFollowerBefore = 'isAFollowerBefore', //"this username was followed page before your comment",
notFollower = "notFollower", //"this username didnt follow page yet", notFollower = 'notFollower', //"this username didnt follow page yet",
isValid = "isValid", //"your comment is valid" isValid = 'isValid', //"your comment is valid"
inValid ="inValid (mentioned before or followed before)" inValid = 'inValid (mentioned before or followed before)',
}
}
\ No newline at end of file
...@@ -2,14 +2,13 @@ import { NestFactory } from '@nestjs/core'; ...@@ -2,14 +2,13 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule,{ cors: true }); const app = await NestFactory.create(AppModule, { cors: true });
app.enableCors({ app.enableCors({
origin: '*', origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
credentials: true, credentials: true,
}) });
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose';
export type RequestDocument = Request & Document export type RequestDocument = Request & Document;
@Schema({timestamps:true}) @Schema({ timestamps: true })
export class Request { export class Request {
@Prop() @Prop()
_id: Types.ObjectId _id: Types.ObjectId;
@Prop() @Prop()
cursor: string cursor: string;
@Prop() @Prop()
type: string type: string;
@Prop() @Prop()
post_short_code: string post_short_code: string;
@Prop() @Prop()
account_username: string account_username: string;
} }
export const RequestSchema = SchemaFactory.createForClass(Request) export const RequestSchema = SchemaFactory.createForClass(Request);
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose' import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose' import { Document, Types } from 'mongoose';
export type ResultDocument = Result & Document export type ResultDocument = Result & Document;
@Schema({ timestamps: true }) @Schema({ timestamps: true })
export class Result { export class Result {
@Prop()
username: string;
@Prop() @Prop()
username: string valid_mentions: number;
@Prop() @Prop()
valid_mentions: number invalid_mentions: number;
@Prop() @Prop()
invalid_mentions: number pending_mentions: number;
@Prop() @Prop()
pending_mentions: number score: number;
@Prop() @Prop()
score: number valid_users: Array<string>;
@Prop()
valid_users: Array<string>
@Prop()
inValid_users: Array<string>
@Prop()
pending_users: Array<string>
@Prop()
inValid_users: Array<string>;
@Prop()
pending_users: Array<string>;
} }
export const ResultSchema = SchemaFactory.createForClass(Result) export const ResultSchema = SchemaFactory.createForClass(Result);
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