Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
shahriar
instagram-service
Commits
22be9bfa
Commit
22be9bfa
authored
Aug 09, 2021
by
soheib
Browse files
add get likes, comments and followers service
parent
c659d81e
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/models/comment.schema.ts
View file @
22be9bfa
...
...
@@ -17,6 +17,9 @@ export class Comment {
@
Prop
({
type
:
Types
.
ObjectId
,
ref
:
'
User
'
})
user_id
:
Types
.
ObjectId
@
Prop
({
type
:
Types
.
ObjectId
,
ref
:
'
Post
'
})
post_id
:
Types
.
ObjectId
@
Prop
()
date
:
number
;
...
...
src/models/user.schema.ts
View file @
22be9bfa
...
...
@@ -3,6 +3,11 @@ import { Document, Types } from 'mongoose';
export
type
UserDocument
=
User
&
Document
;
export
enum
AccountType
{
business
=
"
business
"
,
customer
=
"
customer
"
}
@
Schema
({
timestamps
:
true
})
export
class
User
{
@
Prop
()
...
...
@@ -17,6 +22,8 @@ export class User {
@
Prop
()
full_name
:
string
@
Prop
({
enum
:
AccountType
})
type
:
string
}
export
const
UserSchema
=
SchemaFactory
.
createForClass
(
User
)
\ No newline at end of file
src/service/get-Instagram-data.service.py
View file @
22be9bfa
...
...
@@ -33,17 +33,41 @@ def __main__():
getLikes
(
db
,
post
,
PROFILE
)
# get comments of given post short code
# getComments(db, post)
# getComments(db, post
, PROFILE
)
# get Followers of given profile
# getFollowers(db, profile)
# getFollowers(db, profile
, PROFILE
)
# get tagged Posts of given profile
# getTaggedPosts(db, profile)
def
getComments
(
db
,
post
):
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
})
...
...
@@ -51,8 +75,18 @@ def getComments(db, post):
if
search
is
not
None
and
search
[
"comment_id"
]:
print
(
comment
.
id
,
" Already Exist"
)
else
:
temp
=
{
"comment_id"
:
comment
.
id
,
"owner_username"
:
comment
.
owner
.
username
,
"owner_id"
:
comment
.
owner
.
userid
,
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"
)
...
...
@@ -65,36 +99,49 @@ def getLikes(db, post, profile):
col
=
db
[
"likes"
]
post_col
=
db
[
"posts"
]
account_col
=
db
[
'users'
]
print
(
'start like'
)
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'
)
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
.
shortcode
})
found_post
=
post_col
.
find_one
({
"url"
:
post
_
short
_
code
})
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'
]
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
print
(
account_id
,
post
_id
)
print
(
post
)
print
(
post_short_code
,
'added to
post
s'
)
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
(
{
"username"
:
like
.
username
,
"post_
short_code"
:
post_short_code
})
{
"username"
:
like
.
username
,
"post_
id"
:
post_id
})
try
:
if
search
is
not
None
:
print
(
like
.
username
,
" Already like post : "
,
post_
short_code
)
print
(
like
.
username
,
" Already like post : "
,
post_
id
)
else
:
temp
=
{
"username"
:
like
.
username
,
"post_short_code"
:
post_short_code
}
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
)
...
...
@@ -102,8 +149,19 @@ def getLikes(db, post, profile):
print
(
"Error"
)
def
getFollowers
(
db
,
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
})
...
...
@@ -112,8 +170,19 @@ def getFollowers(db, profile):
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
,
}
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
)
...
...
src/service/getComments.py
0 → 100644
View file @
22be9bfa
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__
()
src/service/getFollowers.py
0 → 100644
View file @
22be9bfa
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__
()
src/service/getLikes.py
0 → 100644
View file @
22be9bfa
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__
()
src/service/instagram.service.ts
View file @
22be9bfa
...
...
@@ -12,6 +12,7 @@ import { FollowerDocument } from '../models/follower.schema';
import
{
LikeDocument
}
from
'
../models/like.schema
'
;
import
{
UserDocument
}
from
'
../models/user.schema
'
;
import
FollowrData
from
'
../values/followers _data
'
import
{
spawn
}
from
'
child_process
'
@
Injectable
()
export
class
InstagramService
implements
OnApplicationBootstrap
{
...
...
@@ -32,7 +33,7 @@ export class InstagramService implements OnApplicationBootstrap {
)
{
}
async
onApplicationBootstrap
()
{
// await this.getFollowersFromJsonData()
}
async
getFollowersFromJsonData
()
{
...
...
@@ -55,6 +56,63 @@ export class InstagramService implements OnApplicationBootstrap {
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
)
{
let
user
:
UserDocument
let
foundUser
=
await
this
.
userModel
.
findOne
({
username
})
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment