Commit c659d81e authored by soheib's avatar soheib
Browse files

add get follower form json data

parent d927093f
......@@ -4,22 +4,9 @@ import { GetUserScore } from './dto/get-user-score';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Post('get-score')
async getUserResults(@Body('username') username: string) {
return await this.appService.getUserScore(username);
}
@Get('calculate-score')
async getFollowers() {
return await this.appService.addResultsToDB();
}
@Get('get-lottory-result')
async getResultDb() {
return await this.appService.getResultDb();
}
constructor(private readonly appService: AppService) { }
}
// @Get('get-followers')
......@@ -57,4 +44,23 @@ export class AppController {
// return await this.appService.addResultsToDB();
// }
\ No newline at end of file
//weakly
// @Post('get-score')
// async getUserResults(@Body('username') username: string) {
// return await this.appService.getUserScore(username);
// }
// @Get('calculate-score')
// async getFollowers() {
// return await this.appService.addResultsToDB();
// }
// @Get('get-lottory-result')
// async getResultDb() {
// return await this.appService.getResultDb();
// }
\ No newline at end of file
......@@ -6,12 +6,17 @@ import { AppService } from './app.service';
import { LikeSchema } from './models/like.schema';
import { CommentSchema } from './models/comment.schema';
import { LottoryResultSchema } from './models/LottoryResult.schema';
import { UserSchema } from './models/user.schema';
import { InstagramService } from './service/instagram.service';
@Module({
imports: [
MongooseModule.forRoot(
'mongodb://netware:Netware%40408009@185.231.180.248:27017/instagram-lottry?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin',
),
MongooseModule.forFeature([
{ name: 'User', schema: UserSchema },
]),
MongooseModule.forFeature([
{ name: 'Follower', schema: FollowerSchema },
]),
......@@ -26,6 +31,6 @@ import { LottoryResultSchema } from './models/LottoryResult.schema';
]),
],
controllers: [AppController],
providers: [AppService],
providers: [AppService, InstagramService],
})
export class AppModule {}
export class AppModule { }
import {
HttpException,
Injectable,
NotFoundException,
OnApplicationBootstrap,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import { CommentDocument } from './models/comment.schema';
import { IFollower } from './interface/Ifollower';
import { IncomingComment } from './interface/IncomingComment';
import { RequestDocument } from './request.schema';
const Instagram = require('instagram-web-api');
const { username, password } = process.env;
import * as _ from 'lodash';
import FollowerPrivateData from './followers_data';
import { CleanedComments, MentionDocument } from './interface/IcleandComment';
import { CommentStatus, UserAllMention } from './interface/UserAllMentions';
import { ResultDocument } from './result.schema';
import { CommentDocument } from './models/comment.schema';
import { LottoryResultDocument } from './models/LottoryResult.schema';
import { FollowerDocument } from './models/follower.schema';
import { LikeDocument } from './models/like.schema';
import addToStoryData from './add-to-story-data'
import { UserDocument } from './models/user.schema';
@Injectable()
export class AppService implements OnApplicationBootstrap {
client: any;
constructor(
@InjectModel('Follower')
private userModel: Model<UserDocument>,
@InjectModel('Follower')
private followerModel: Model<FollowerDocument>,
@InjectModel('Like')
......@@ -40,126 +31,10 @@ export class AppService implements OnApplicationBootstrap {
) { }
async onApplicationBootstrap() {
// this.client = await this.login('shahriarvelayat', 'shve8864@@');
this.client = null;
}
async getUserLikesScore(username: string) {
// let foundFollower = await this.followerModel.findOne({ username })
// if (foundFollower) {
let foundLikes = await this.likeModel.find({ username })
return foundLikes.length
// }
// else {
// throw new HttpException(`you are not a follower of this page`, 404)
// }
}
async getUserCommentsScore(username: string) {
let foundUserPosts = await this.commentModel.distinct('post_short_code')
let userCommentScore = new Array<{ post: string, post_score: number }>()
foundUserPosts.forEach((post) => {
userCommentScore.push({
post,
post_score: 0
})
})
let foundUserComments = await this.commentModel.find({ owner_username: username })
for await (const comment of foundUserComments) {
userCommentScore.forEach(element => {
if (comment.post_short_code === element.post && element.post_score < 3) {
element.post_score++
}
})
}
let score = 0
userCommentScore.forEach(element => {
score += element.post_score
})
return score
}
async getTagsScore(username: string) {
let addToStoryScore = 0
let foundUser = addToStoryData.addToStoryData.find((user) => {
return user.username === username
})
if (foundUser) {
addToStoryScore = foundUser.count
}
return addToStoryScore
}
async calculateUserScore(username: string) {
let likesScore = await this.getUserLikesScore(username)
let commentScore = await this.getUserCommentsScore(username)
let addToStoryScore = await this.getTagsScore(username)
return likesScore + commentScore + addToStoryScore
}
async getUserScore(username: string) {
let likesScore = await this.getUserLikesScore(username)
let commentScore = await this.getUserCommentsScore(username)
let addToStoryScore = await this.getTagsScore(username)
return {likesScore , commentScore , addToStoryScore, totalScore:likesScore + commentScore + addToStoryScore }
}
async addResultsToDB() {
await this.lottryResultModel.deleteMany({})
let foundUsernameInComment = await this.commentModel.distinct('owner_username')
let foundUsernameInLike = await this.likeModel.distinct('username')
let foundUsernames = new Array<string>()
foundUsernameInComment.forEach((username) => {
if (!foundUsernames.includes(username)) {
foundUsernames.push(username)
}
})
foundUsernameInLike.forEach((username) => {
if (!foundUsernames.includes(username)) {
foundUsernames.push(username)
}
})
console.log(foundUsernames);
const comptitionArray = new Array<any>();
let index = 1000;
let totalItems = foundUsernames.length
let count = 0
for await (const username of foundUsernames) {
let userScore = await this.calculateUserScore(username)
for (let u = 0; u < userScore; u++) {
comptitionArray.push({
index,
username: username,
})
index++;
console.log(`username:${username} , index: ${index}, score:${userScore}`);
}
console.log(`user index:${count} , total items: ${totalItems}`);
count++
}
await this.lottryResultModel.insertMany(comptitionArray);
return 'successfull';
}
async getResultDb() {
return await this.lottryResultModel
.find()
.select({ username: 1, index: 1});
}
}
......@@ -717,7 +592,7 @@ export class AppService implements OnApplicationBootstrap {
// return 'successfull';
// }
// export class ResultResponse {
......@@ -731,3 +606,133 @@ export class AppService implements OnApplicationBootstrap {
// users?: Array<any>;
// lottory_chances_codes: Array<string>;
// }
/// weakly
// async getUserLikesScore(username: string) {
// // let foundFollower = await this.followerModel.findOne({ username })
// // if (foundFollower) {
// let foundLikes = await this.likeModel.find({ username })
// return foundLikes.length
// // }
// // else {
// // throw new HttpException(`you are not a follower of this page`, 404)
// // }
// }
// async getUserCommentsScore(username: string) {
// let foundUserPosts = await this.commentModel.distinct('post_short_code')
// let userCommentScore = new Array<{ post: string, post_score: number }>()
// foundUserPosts.forEach((post) => {
// userCommentScore.push({
// post,
// post_score: 0
// })
// })
// let foundUserComments = await this.commentModel.find({ owner_username: username })
// for await (const comment of foundUserComments) {
// userCommentScore.forEach(element => {
// if (comment.post_short_code === element.post && element.post_score < 3) {
// element.post_score++
// }
// })
// }
// let score = 0
// userCommentScore.forEach(element => {
// score += element.post_score
// })
// return score
// }
// async getTagsScore(username: string) {
// let addToStoryScore = 0
// let foundUser = addToStoryData.addToStoryData.find((user) => {
// return user.username === username
// })
// if (foundUser) {
// addToStoryScore = foundUser.count
// }
// return addToStoryScore
// }
// async calculateUserScore(username: string) {
// let likesScore = await this.getUserLikesScore(username)
// let commentScore = await this.getUserCommentsScore(username)
// let addToStoryScore = await this.getTagsScore(username)
// return likesScore + commentScore + addToStoryScore
// }
// async getUserScore(username: string) {
// let likesScore = await this.getUserLikesScore(username)
// let commentScore = await this.getUserCommentsScore(username)
// let addToStoryScore = await this.getTagsScore(username)
// return { likesScore, commentScore, addToStoryScore, totalScore: likesScore + commentScore + addToStoryScore }
// }
// async addResultsToDB() {
// await this.lottryResultModel.deleteMany({})
// let foundUsernameInComment = await this.commentModel.distinct('owner_username')
// let foundUsernameInLike = await this.likeModel.distinct('username')
// let foundUsernames = new Array<string>()
// foundUsernameInComment.forEach((username) => {
// if (!foundUsernames.includes(username)) {
// foundUsernames.push(username)
// }
// })
// foundUsernameInLike.forEach((username) => {
// if (!foundUsernames.includes(username)) {
// foundUsernames.push(username)
// }
// })
// console.log(foundUsernames);
// const comptitionArray = new Array<any>();
// let index = 1000;
// let totalItems = foundUsernames.length
// let count = 0
// for await (const username of foundUsernames) {
// let userScore = await this.calculateUserScore(username)
// for (let u = 0; u < userScore; u++) {
// comptitionArray.push({
// index,
// username: username,
// })
// index++;
// console.log(`username:${username} , index: ${index}, score:${userScore}`);
// }
// console.log(`user index:${count} , total items: ${totalItems}`);
// count++
// }
// await this.lottryResultModel.insertMany(comptitionArray);
// return 'successfull';
// }
// async getResultDb() {
// return await this.lottryResultModel
// .find()
// .select({ username: 1, index: 1 });
// }
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
......@@ -9,9 +9,9 @@ async function bootstrap() {
credentials: true,
})
await app.listen(4001).then(()=>{
console.log(`server start on port 4001`);
await app.listen(4002).then(() => {
console.log(`server start on port 4002`);
})
}
bootstrap();
......@@ -14,19 +14,13 @@ export class Comment {
@Prop()
text: string;
@Prop()
owner_username: string;
@Prop()
owner_id: string;
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
@Prop()
date: number;
@Prop()
post_short_code: string;
@Prop({ type: Object })
comment_object: Object;
}
export const CommentSchema = SchemaFactory.createForClass(Comment);
export const CommentSchema = SchemaFactory.createForClass(Comment);
\ No newline at end of file
......@@ -8,17 +8,11 @@ export class Follower {
@Prop()
_id: Types.ObjectId;
@Prop()
username: string;
@Prop()
full_name: string;
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
@Prop()
profile_pic: string;
@Prop()
account: string;
@Prop({ type: Types.ObjectId, ref: 'User' })
account_id: Types.ObjectId
@Prop()
follow_date: number;
......
......@@ -8,11 +8,11 @@ export class Like {
@Prop()
_id: Types.ObjectId;
@Prop()
username: string;
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
@Prop()
post_short_code: string;
@Prop({ type: Types.ObjectId, ref: 'Post' })
post_id: Types.ObjectId
}
export const LikeSchema =
......
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type PostDocument = Post & Document;
@Schema({ timestamps: true })
export class Post {
@Prop()
_id: Types.ObjectId
@Prop()
url: string
@Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId
}
export const PostSchema =
SchemaFactory.createForClass(Post)
\ No newline at end of file
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
export type UserDocument = User & Document;
@Schema({ timestamps: true })
export class User {
@Prop()
_id: Types.ObjectId
@Prop()
username: string
@Prop()
instagram_user_id: string
@Prop()
full_name: string
}
export const UserSchema =
SchemaFactory.createForClass(User)
\ No newline at end of file
import instaloader
import pymongo
username = "shahriarvelayat"
password = "pj!GorO!0oWo$t6f"
username = "kakasrm"
password = "kaka1374"
mongo_connection_string = "mongodb://azadi:azadi%404010@185.231.180.248:27017/?serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256"
database_name = "azadi-gold-backend"
post_short_code = "CSCUev0swJO"
......@@ -30,16 +30,16 @@ def __main__():
db = client[database_name]
# get likes of given post short code
getLikes(db, post);
getLikes(db, post, PROFILE)
# get comments of given post short code
getComments(db, post);
# getComments(db, post)
# get Followers of given profile
getFollowers(db, profile);
# getFollowers(db, profile)
# get tagged Posts of given profile
getTaggedPosts(db, profile);
# getTaggedPosts(db, profile)
def getComments(db, post):
......@@ -61,16 +61,40 @@ def getComments(db, post):
print("Error")
def getLikes(db, post):
def getLikes(db, post, profile):
col = db["likes"]
post_col = db["posts"]
account_col = db['users']
print('start like')
account_id = None
post_id = None
found_account = account_col.find_one({"username": profile},)
if found_account is not None:
print(found_account.username, 'is already exists')
account_id = found_account['_id']
else:
created_account = account_col.insert_one({"username": profile})
account_id = created_account.inserted_id
found_post = post_col.find_one({"url": post.shortcode})
if found_post is not None:
print(found_post.url, 'is already exists')
post_id = found_post['_id']
else:
created_post = post_col.insert_one({"url": post.shortcode})
post_id = created_post.inserted_id
print(account_id, post_id)
print(post)
for like in post.get_likes():
print("Searching : ", like.username, post_short_code)
search = col.find_one({"username": like.username, "post_short_code": post_short_code})
search = col.find_one(
{"username": like.username, "post_short_code": post_short_code})
try:
if search is not None:
print(like.username, " Already like post : ", post_short_code)
else:
temp = {"username": like.username, "post_short_code": post_short_code}
temp = {"username": like.username,
"post_short_code": post_short_code}
x = col.update(temp, temp, upsert=True)
print(like.username, " like post : ", post_short_code)
......@@ -85,7 +109,8 @@ def getFollowers(db, profile):
search = col.find_one({"username": follower.username})
try:
if search is not None:
print(follower.username, " Already Exist in : ", profile, "Followers")
print(follower.username, " Already Exist in : ",
profile, "Followers")
else:
temp = {"username": follower.username, "user_id": follower.userid, "full_name": follower.full_name,
"business_username": PROFILE, "follow_date": "", "profile_pic": follower.profile_pic_url, }
......@@ -99,7 +124,8 @@ def getFollowers(db, profile):
def getTaggedPosts(db, profile):
col = db["tagged"]
for tagged_post in profile.get_tagged_posts():
print("Searching in ", profile, " for tagged post with id :", tagged_post.shortcode)
print("Searching in ", profile,
" for tagged post with id :", tagged_post.shortcode)
search = col.find_one({"short_code": tagged_post.shortcode})
try:
if search is not None:
......@@ -115,4 +141,4 @@ def getTaggedPosts(db, profile):
print("Error")
__main__();
__main__()
import {
HttpException,
Injectable,
OnApplicationBootstrap,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model, Types } from 'mongoose';
import * as _ from 'lodash';
import { CommentDocument } from '../models/comment.schema';
import { LottoryResultDocument } from '../models/LottoryResult.schema';
import { FollowerDocument } from '../models/follower.schema';
import { LikeDocument } from '../models/like.schema';
import { UserDocument } from '../models/user.schema';
import FollowrData from '../values/followers _data'
@Injectable()
export class InstagramService implements OnApplicationBootstrap {
constructor(
@InjectModel('User')
private userModel: Model<UserDocument>,
@InjectModel('Follower')
private followerModel: Model<FollowerDocument>,
@InjectModel('Like')
private likeModel: Model<LikeDocument>,
@InjectModel('Comment')
private commentModel: Model<CommentDocument>,
@InjectModel('LottryResult')
private lottryResultModel: Model<LottoryResultDocument>,
) { }
async onApplicationBootstrap() {
// await this.getFollowersFromJsonData()
}
async getFollowersFromJsonData() {
console.log('start proccess...');
let accountUsername = FollowrData.account_username
let account = await this.findOrCreateUser(accountUsername)
for await (const follower of FollowrData.relationships_followers) {
let user = await this.findOrCreateUser(follower.string_list_data[0].value.toString())
let foundFollower = await this.followerModel.findOne({ $and: [{ user_id: user._id }, { account_id: account._id }] })
if (!foundFollower) {
let importedFollower = await this.followerModel.create({
_id: Types.ObjectId(),
user_id: user._id,
account_id: account._id,
follow_date: follower.string_list_data[0].timestamp
})
}
}
console.log('end of proccess...');
}
async findOrCreateUser(username: string) {
let user: UserDocument
let foundUser = await this.userModel.findOne({ username })
if (!foundUser) {
let createdUser = await this.userModel.create({
_id: Types.ObjectId(),
username: username
})
user = createdUser
}
else {
user = foundUser
}
return user
}
}
This diff is collapsed.
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