Commit 22be9bfa authored by soheib's avatar soheib
Browse files

add get likes, comments and followers service

parent c659d81e
...@@ -17,6 +17,9 @@ export class Comment { ...@@ -17,6 +17,9 @@ export class Comment {
@Prop({ type: Types.ObjectId, ref: 'User' }) @Prop({ type: Types.ObjectId, ref: 'User' })
user_id: Types.ObjectId user_id: Types.ObjectId
@Prop({ type: Types.ObjectId, ref: 'Post' })
post_id: Types.ObjectId
@Prop() @Prop()
date: number; date: number;
......
...@@ -3,6 +3,11 @@ import { Document, Types } from 'mongoose'; ...@@ -3,6 +3,11 @@ import { Document, Types } from 'mongoose';
export type UserDocument = User & Document; export type UserDocument = User & Document;
export enum AccountType {
business = "business",
customer = "customer"
}
@Schema({ timestamps: true }) @Schema({ timestamps: true })
export class User { export class User {
@Prop() @Prop()
...@@ -17,6 +22,8 @@ export class User { ...@@ -17,6 +22,8 @@ export class User {
@Prop() @Prop()
full_name: string full_name: string
@Prop({ enum: AccountType })
type: string
} }
export const UserSchema = export const UserSchema =
SchemaFactory.createForClass(User) SchemaFactory.createForClass(User)
\ No newline at end of file
...@@ -33,17 +33,41 @@ def __main__(): ...@@ -33,17 +33,41 @@ def __main__():
getLikes(db, post, PROFILE) getLikes(db, post, PROFILE)
# get comments of given post short code # get comments of given post short code
# getComments(db, post) # getComments(db, post, PROFILE)
# get Followers of given profile # get Followers of given profile
# getFollowers(db, profile) # getFollowers(db, profile, PROFILE)
# get tagged Posts of given profile # get tagged Posts of given profile
# getTaggedPosts(db, profile) # getTaggedPosts(db, profile)
def getComments(db, post): def getComments(db, post, profile):
col = db["comments"] col = db["comments"]
post_col = db["posts"]
account_col = db['users']
print('start getting comments')
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
print(profile, 'added to users')
found_post = post_col.find_one({"url": post_short_code})
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_short_code, "user_id": account_id})
post_id = created_post.inserted_id
print(post_short_code, 'added to posts')
for comment in post.get_comments(): for comment in post.get_comments():
print("Searching : ", comment.id) print("Searching : ", comment.id)
search = col.find_one({"comment_id": comment.id}) search = col.find_one({"comment_id": comment.id})
...@@ -51,8 +75,18 @@ def getComments(db, post): ...@@ -51,8 +75,18 @@ def getComments(db, post):
if search is not None and search["comment_id"]: if search is not None and search["comment_id"]:
print(comment.id, " Already Exist") print(comment.id, " Already Exist")
else: else:
temp = {"comment_id": comment.id, "owner_username": comment.owner.username, user_id = None
"owner_id": comment.owner.userid, found_username = account_col.find_one({'username': comment.owner.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one({"username": comment.owner.username,
"instagram_user_id": comment.owner.userid})
user_id = created_user.inserted_id
print(comment.owner.username, 'added to users')
temp = {"comment_id": comment.id, "user_id": user_id, "post_id": post_id,
"text": comment.text, "date": comment.created_at_utc.timestamp()} "text": comment.text, "date": comment.created_at_utc.timestamp()}
x = col.update(temp, temp, upsert=True) x = col.update(temp, temp, upsert=True)
print(comment.id, " added") print(comment.id, " added")
...@@ -65,36 +99,49 @@ def getLikes(db, post, profile): ...@@ -65,36 +99,49 @@ def getLikes(db, post, profile):
col = db["likes"] col = db["likes"]
post_col = db["posts"] post_col = db["posts"]
account_col = db['users'] account_col = db['users']
print('start like') print('start getting likes')
account_id = None account_id = None
post_id = None post_id = None
found_account = account_col.find_one({"username": profile},) found_account = account_col.find_one({"username": profile},)
if found_account is not None: if found_account is not None:
print(found_account.username, 'is already exists') print(found_account['username'], 'is already exists')
account_id = found_account['_id'] account_id = found_account['_id']
else: else:
created_account = account_col.insert_one({"username": profile}) created_account = account_col.insert_one({"username": profile})
account_id = created_account.inserted_id account_id = created_account.inserted_id
print(profile, 'added to users')
found_post = post_col.find_one({"url": post.shortcode}) found_post = post_col.find_one({"url": post_short_code})
if found_post is not None: if found_post is not None:
print(found_post.url, 'is already exists') print(found_post['url'], 'is already exists')
post_id = found_post['_id'] post_id = found_post['_id']
else: else:
created_post = post_col.insert_one({"url": post.shortcode}) created_post = post_col.insert_one({"url": post_short_code, "user_id": account_id})
post_id = created_post.inserted_id post_id = created_post.inserted_id
print(account_id, post_id) print(post_short_code, 'added to posts')
print(post)
for like in post.get_likes(): for like in post.get_likes():
print("Searching : ", like.username, post_short_code) print('======', like)
print("Searching : ", like.username, post_id)
search = col.find_one( search = col.find_one(
{"username": like.username, "post_short_code": post_short_code}) {"username": like.username, "post_id": post_id})
try: try:
if search is not None: if search is not None:
print(like.username, " Already like post : ", post_short_code) print(like.username, " Already like post : ", post_id)
else: else:
temp = {"username": like.username, user_id = None
"post_short_code": post_short_code} found_username = account_col.find_one({'username': like.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": like.username})
user_id = created_user.inserted_id
print(like.username, 'added to users')
temp = {"user_id": user_id,
"post_id": post_id}
x = col.update(temp, temp, upsert=True) x = col.update(temp, temp, upsert=True)
print(like.username, " like post : ", post_short_code) print(like.username, " like post : ", post_short_code)
...@@ -102,8 +149,19 @@ def getLikes(db, post, profile): ...@@ -102,8 +149,19 @@ def getLikes(db, post, profile):
print("Error") print("Error")
def getFollowers(db, profile): def getFollowers(db, profile, profile_username):
col = db["followers"] col = db["followers"]
account_col = db['users']
account_id = None
found_username = account_col.find_one({'username': profile_username})
if found_username is not None:
print(found_username['username'], 'is already exists')
account_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": profile_username, type: "business"})
account_id = created_user.inserted_id
print(profile_username, 'added to users')
for follower in profile.get_followers(): for follower in profile.get_followers():
print("Searching in ", profile, " for :", follower.username) print("Searching in ", profile, " for :", follower.username)
search = col.find_one({"username": follower.username}) search = col.find_one({"username": follower.username})
...@@ -112,8 +170,19 @@ def getFollowers(db, profile): ...@@ -112,8 +170,19 @@ def getFollowers(db, profile):
print(follower.username, " Already Exist in : ", print(follower.username, " Already Exist in : ",
profile, "Followers") profile, "Followers")
else: else:
temp = {"username": follower.username, "user_id": follower.userid, "full_name": follower.full_name, user_id = None
"business_username": PROFILE, "follow_date": "", "profile_pic": follower.profile_pic_url, } found_username = account_col.find_one({'username': follower.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": follower.username,
"instagram_user_id": follower.userid, "full_name": follower.full_name})
user_id = created_user.inserted_id
print(follower.username, 'added to users')
temp = {"username": follower.username, "user_id": user_id,
"account_id": account_id, "follow_date": "", "profile_pic": follower.profile_pic_url, }
x = col.update(temp, temp, upsert=True) x = col.update(temp, temp, upsert=True)
print(follower.username, " followed : ", profile) print(follower.username, " followed : ", profile)
......
import instaloader
import pymongo
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
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 = str(sys.argv[3])
PROFILE = str(sys.argv[4])
def __main__():
print("connecting to the instagram ....")
L = instaloader.Instaloader()
L.login(username, password)
profile = instaloader.Profile.from_username(L.context, PROFILE)
print("connected to the instagram :) ")
print("Load post data ....")
post = instaloader.Post.from_shortcode(L.context, post_short_code)
print("Post data loaded :) ")
print("connecting to the database ....")
client = pymongo.MongoClient(mongo_connection_string)
db = client[database_name]
# get comments of given post short code
getComments(db, post, PROFILE)
def getComments(db, post, profile):
col = db["comments"]
post_col = db["posts"]
account_col = db['users']
print('start getting comments')
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
print(profile, 'added to users')
found_post = post_col.find_one({"url": post_short_code})
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_short_code, "user_id": account_id})
post_id = created_post.inserted_id
print(post_short_code, 'added to posts')
for comment in post.get_comments():
print("Searching : ", comment.id)
search = col.find_one({"comment_id": comment.id})
try:
if search is not None and search["comment_id"]:
print(comment.id, " Already Exist")
else:
user_id = None
found_username = account_col.find_one(
{'username': comment.owner.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one({"username": comment.owner.username,
"instagram_user_id": comment.owner.userid})
user_id = created_user.inserted_id
print(comment.owner.username, 'added to users')
temp = {"comment_id": comment.id, "user_id": user_id, "post_id": post_id,
"text": comment.text, "date": comment.created_at_utc.timestamp()}
x = col.update(temp, temp, upsert=True)
print(comment.id, " added")
except:
print("Error")
__main__()
import instaloader
import pymongo
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
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"
PROFILE = str(sys.argv[3])
def __main__():
print("connecting to the instagram ....")
L = instaloader.Instaloader()
L.login(username, password)
profile = instaloader.Profile.from_username(L.context, PROFILE)
print("connected to the instagram :) ")
print("Load post data ....")
# post = instaloader.Post.from_shortcode(L.context, post_short_code)
print("Post data loaded :) ")
print("connecting to the database ....")
client = pymongo.MongoClient(mongo_connection_string)
db = client[database_name]
# get Followers of given profile
getFollowers(db, profile, PROFILE)
def getFollowers(db, profile, profile_username):
col = db["followers"]
account_col = db['users']
account_id = None
found_username = account_col.find_one({'username': profile_username})
if found_username is not None:
print(found_username['username'], 'is already exists')
account_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": profile_username, type: "business"})
account_id = created_user.inserted_id
print(profile_username, 'added to users')
for follower in profile.get_followers():
print("Searching in ", profile, " for :", follower.username)
search = col.find_one({"username": follower.username})
try:
if search is not None:
print(follower.username, " Already Exist in : ",
profile, "Followers")
else:
user_id = None
found_username = account_col.find_one(
{'username': follower.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": follower.username,
"instagram_user_id": follower.userid, "full_name": follower.full_name})
user_id = created_user.inserted_id
print(follower.username, 'added to users')
temp = {"username": follower.username, "user_id": user_id,
"account_id": account_id, "follow_date": "", "profile_pic": follower.profile_pic_url, }
x = col.update(temp, temp, upsert=True)
print(follower.username, " followed : ", profile)
except:
print("Error")
__main__()
import instaloader
import pymongo
import sys
username = str(sys.argv[1])
password = str(sys.argv[2])
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 = str(sys.argv[3])
PROFILE = str(sys.argv[4])
def __main__():
print(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])
print("connecting to the instagram ....")
L = instaloader.Instaloader()
L.login(username, password)
profile = instaloader.Profile.from_username(L.context, PROFILE)
print("connected to the instagram :) ")
print("Load post data ....")
post = instaloader.Post.from_shortcode(L.context, post_short_code)
print("Post data loaded :) ")
print("connecting to the database ....")
client = pymongo.MongoClient(mongo_connection_string)
db = client[database_name]
# get likes of given post short code
getLikes(db, post, PROFILE)
def getLikes(db, post, profile):
col = db["likes"]
post_col = db["posts"]
account_col = db['users']
print('start getting likes')
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
print(profile, 'added to users')
found_post = post_col.find_one({"url": post_short_code})
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_short_code, "user_id": account_id})
post_id = created_post.inserted_id
print(post_short_code, 'added to posts')
for like in post.get_likes():
print('======', like)
print("Searching : ", like.username, post_id)
search = col.find_one(
{"username": like.username, "post_id": post_id})
try:
if search is not None:
print(like.username, " Already like post : ", post_id)
else:
user_id = None
found_username = account_col.find_one(
{'username': like.username})
if found_username is not None:
print(found_username['username'], 'is already exists')
user_id = found_username['_id']
else:
created_user = account_col.insert_one(
{"username": like.username})
user_id = created_user.inserted_id
print(like.username, 'added to users')
temp = {"user_id": user_id,
"post_id": post_id}
x = col.update(temp, temp, upsert=True)
print(like.username, " like post : ", post_short_code)
except:
print("Error")
__main__()
...@@ -12,6 +12,7 @@ import { FollowerDocument } from '../models/follower.schema'; ...@@ -12,6 +12,7 @@ import { FollowerDocument } from '../models/follower.schema';
import { LikeDocument } from '../models/like.schema'; import { LikeDocument } from '../models/like.schema';
import { UserDocument } from '../models/user.schema'; import { UserDocument } from '../models/user.schema';
import FollowrData from '../values/followers _data' import FollowrData from '../values/followers _data'
import { spawn } from 'child_process'
@Injectable() @Injectable()
export class InstagramService implements OnApplicationBootstrap { export class InstagramService implements OnApplicationBootstrap {
...@@ -32,7 +33,7 @@ export class InstagramService implements OnApplicationBootstrap { ...@@ -32,7 +33,7 @@ export class InstagramService implements OnApplicationBootstrap {
) { } ) { }
async onApplicationBootstrap() { async onApplicationBootstrap() {
// await this.getFollowersFromJsonData()
} }
async getFollowersFromJsonData() { async getFollowersFromJsonData() {
...@@ -55,6 +56,63 @@ export class InstagramService implements OnApplicationBootstrap { ...@@ -55,6 +56,63 @@ export class InstagramService implements OnApplicationBootstrap {
console.log('end of proccess...'); console.log('end of proccess...');
} }
async getLikesFromInstaLoader(username: string, password: string, post_short_code: string, profile: string) {
const getLikesProccess = spawn('python3', ['src/service/getLikes.py', `${username}`, `${password}`, `${post_short_code}`, `${profile}`,]);
getLikesProccess.stdout.on('data', function (data) {
console.log('start collecting likes from python script ...');
console.log(data.toString())
});
getLikesProccess.on('error', (err) => {
console.log(`child process has error ${err}`);
return err
});
getLikesProccess.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
return "successfull"
});
}
async getCommentsFromInstaLoader(username: string, password: string, post_short_code: string, profile: string) {
const getCommentsProccess = spawn('python3', ['src/service/getComments.py', `${username}`, `${password}`, `${post_short_code}`, `${profile}`]);
getCommentsProccess.stdout.on('data', function (data) {
console.log('start collecting comments from python script ...');
console.log(data.toString())
});
getCommentsProccess.on('error', (err) => {
console.log(`child process has error ${err}`);
return err
});
getCommentsProccess.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
return "successfull"
});
}
async getFollowersFromInstaLoader(username: string, password: string, profile: string) {
const getFollowersProccess = spawn('python3', ['src/service/getFollowers.py', `${username}`, `${password}`, `${profile}`]);
getFollowersProccess.stdout.on('data', function (data) {
console.log('start collecting followers from python script ...');
console.log(data.toString())
});
getFollowersProccess.on('error', (err) => {
console.log(`child process has error ${err}`);
return err
});
getFollowersProccess.on('close', (code) => {
console.log(`child process close all stdio with code ${code}`);
return "successfull"
});
}
async findOrCreateUser(username: string) { async findOrCreateUser(username: string) {
let user: UserDocument let user: UserDocument
let foundUser = await this.userModel.findOne({ username }) let foundUser = await this.userModel.findOne({ username })
......
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