Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2021-155
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2021-155
2021-155
Commits
0a8c44ae
Commit
0a8c44ae
authored
Nov 19, 2021
by
dasunx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Telegram connect backend
parent
c0aaece6
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
213 additions
and
51 deletions
+213
-51
backend/controllers/users.js
backend/controllers/users.js
+121
-2
backend/models/user.js
backend/models/user.js
+3
-0
backend/python/auto-answer/Dev.py
backend/python/auto-answer/Dev.py
+13
-10
backend/python/auto-answer/Medium.py
backend/python/auto-answer/Medium.py
+13
-10
backend/python/auto-answer/github.py
backend/python/auto-answer/github.py
+15
-8
backend/python/auto-answer/scrapper.py
backend/python/auto-answer/scrapper.py
+6
-4
backend/python/auto-answer/stof.py
backend/python/auto-answer/stof.py
+13
-3
backend/python/auto-answer/youtube.py
backend/python/auto-answer/youtube.py
+1
-1
backend/requirements.txt
backend/requirements.txt
+10
-11
backend/routes.js
backend/routes.js
+11
-2
backend/utils/telegram.js
backend/utils/telegram.js
+7
-0
No files found.
backend/controllers/users.js
View file @
0a8c44ae
...
...
@@ -3,6 +3,7 @@ const jwtDecode = require('jwt-decode');
const
{
body
,
validationResult
}
=
require
(
'
express-validator
'
);
const
{
createToken
,
hashPassword
,
verifyPassword
}
=
require
(
'
../utils/authentication
'
);
const
{
create6DigitCode
}
=
require
(
'
../utils/telegram
'
);
exports
.
signup
=
async
(
req
,
res
)
=>
{
const
result
=
validationResult
(
req
);
...
...
@@ -90,8 +91,8 @@ exports.authenticate = async (req, res) => {
const
token
=
createToken
(
user
);
const
decodedToken
=
jwtDecode
(
token
);
const
expiresAt
=
decodedToken
.
exp
;
const
{
username
,
role
,
id
,
created
,
profilePhoto
}
=
user
;
const
userInfo
=
{
username
,
role
,
id
,
created
,
profilePhoto
};
const
{
username
,
role
,
id
,
created
,
profilePhoto
,
isTelegramConnected
}
=
user
;
const
userInfo
=
{
username
,
role
,
id
,
created
,
profilePhoto
,
isTelegramConnected
};
res
.
json
({
message
:
'
Authentication successful!
'
,
...
...
@@ -139,6 +140,124 @@ exports.find = async (req, res, next) => {
}
};
exports
.
createTelegramValidateCode
=
async
(
req
,
res
,
next
)
=>
{
try
{
const
{
username
}
=
req
.
body
;
const
user
=
await
User
.
findOne
({
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
json
({
message
:
'
User not found.
'
});
}
if
(
user
.
isTelegramConnected
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Telegram already connected.
'
});
}
const
telegramValidationCode
=
create6DigitCode
();
user
.
telegramConnectionCode
=
telegramValidationCode
;
await
user
.
save
();
return
res
.
status
(
200
).
json
({
user
:
user
});
}
catch
(
error
)
{
next
(
error
);
}
};
exports
.
connectWithTelegram
=
async
(
req
,
res
,
next
)
=>
{
try
{
const
{
username
,
telegramValidationCode
,
telegramId
}
=
req
.
body
;
console
.
log
(
username
);
const
user
=
await
User
.
findOne
({
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
json
({
message
:
'
User not found.
'
});
}
if
(
user
.
isTelegramConnected
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Telegram already connected.
'
});
}
if
(
user
.
telegramConnectionCode
!==
telegramValidationCode
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Wrong validation code.
'
});
}
user
.
telegramConnectionCode
=
''
;
user
.
isTelegramConnected
=
true
;
user
.
telegramId
=
telegramId
;
await
user
.
save
();
return
res
.
status
(
200
).
json
({
user
:
user
});
}
catch
(
error
)
{
next
(
error
);
}
};
exports
.
verifyTelegramConnection
=
async
(
req
,
res
,
next
)
=>
{
try
{
const
{
username
}
=
req
.
body
;
const
user
=
await
User
.
findOne
({
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
json
({
message
:
'
User not found.
'
});
}
if
(
!
user
.
isTelegramConnected
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Telegram not connected.
'
});
}
return
res
.
status
(
200
).
json
({
user
:
user
});
}
catch
(
error
)
{
next
(
error
);
}
};
exports
.
verifyTelegramUsername
=
async
(
req
,
res
,
next
)
=>
{
try
{
const
{
username
,
telegramId
}
=
req
.
body
;
const
user
=
await
User
.
findOne
({
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
json
({
message
:
'
User not found.
'
});
}
if
(
!
user
.
isTelegramConnected
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Telegram not connected.
'
});
}
if
(
user
.
telegramId
!==
telegramId
)
{
return
res
.
status
(
400
).
json
({
message
:
'
Wrong telegram id.
'
});
}
const
token
=
createToken
(
user
);
return
res
.
status
(
200
).
json
({
user
:
user
,
token
});
}
catch
(
error
)
{
next
(
error
);
}
};
exports
.
disconnectTelegram
=
async
(
req
,
res
,
next
)
=>
{
try
{
const
{
username
}
=
req
.
body
;
const
user
=
await
User
.
findOne
({
username
});
if
(
!
user
)
{
return
res
.
status
(
400
).
json
({
message
:
'
User not found.
'
});
}
user
.
isTelegramConnected
=
false
;
user
.
telegramId
=
''
;
await
user
.
save
();
return
res
.
status
(
200
).
json
({
user
:
user
});
}
catch
(
error
)
{
next
(
error
);
}
};
exports
.
validateUser
=
[
body
(
'
username
'
)
.
exists
()
...
...
backend/models/user.js
View file @
0a8c44ae
...
...
@@ -11,6 +11,9 @@ const userModel = new Schema({
return
`https://secure.gravatar.com/ad/
${
this
.
_id
}
?s=90&d=monsterid`
;
}
},
isTelegramConnected
:
{
type
:
Boolean
,
default
:
false
},
telegramId
:
{
type
:
String
,
default
:
''
},
telegramConnectionCode
:
{
type
:
Number
,
default
:
111111
},
created
:
{
type
:
Date
,
default
:
Date
.
now
}
});
...
...
backend/python/auto-answer/Dev.py
View file @
0a8c44ae
...
...
@@ -47,7 +47,6 @@ class DevTo:
for
url
in
validUrls
:
try
:
vset
=
{}
print
(
url
)
username
=
re
.
search
(
r"https://dev.to/([^/?]+)"
,
url
)
.
group
(
1
)
tag
=
re
.
search
(
r"https://dev.to/([^/?]+)/([^/?]+)"
,
url
)
.
group
(
2
)
vset
[
"username"
]
=
username
...
...
@@ -84,12 +83,16 @@ class DevTo:
get content of the valid urls
return the content of valid dev.to articles
"""
links
=
self
.
google
(
f
"site:dev.to {self.title} after:2020-01-01"
)
validUrls
=
self
.
get_valid_urls
(
links
)
validSets
=
self
.
get_valid_sets
(
validUrls
)
blogs
=
[]
for
validset
in
validSets
:
blog
=
self
.
get_blogs
(
validset
[
"username"
],
validset
[
"tag"
])
if
bool
(
blog
):
blogs
.
append
(
blog
)
return
{
"blogs"
:
blogs
,
"resources"
:
validUrls
}
try
:
links
=
self
.
google
(
f
"site:dev.to {self.title} after:2020-01-01"
)
validUrls
=
self
.
get_valid_urls
(
links
)
validSets
=
self
.
get_valid_sets
(
validUrls
)
blogs
=
[]
for
validset
in
validSets
:
blog
=
self
.
get_blogs
(
validset
[
"username"
],
validset
[
"tag"
])
if
bool
(
blog
):
blogs
.
append
(
blog
)
return
{
"blogs"
:
blogs
,
"resources"
:
validUrls
}
except
Exception
as
e
:
print
(
e
)
return
{
"blogs"
:
[],
"resources"
:
[]}
\ No newline at end of file
backend/python/auto-answer/Medium.py
View file @
0a8c44ae
...
...
@@ -52,7 +52,6 @@ class Medium:
for
url
in
validUrls
:
try
:
vset
=
{}
print
(
url
)
username
=
re
.
search
(
r"https://medium.com/([^/?]+)"
,
url
)
.
group
(
1
)
tag
=
re
.
search
(
r"https://medium.com/([^/?]+)/([^/?]+)"
,
url
)
.
group
(
2
)
vset
[
"username"
]
=
username
...
...
@@ -85,12 +84,16 @@ class Medium:
"""
return a list of articles and/or resources
"""
links
=
self
.
google
(
f
"site:medium.com {self.title} after:2020-01-01"
)
validUrls
=
self
.
getValidUrls
(
links
)
validSets
=
self
.
getValidSets
(
validUrls
)
blogs
=
[]
for
validset
in
validSets
:
blog
=
self
.
getBlogs
(
validset
[
"username"
],
validset
[
"tag"
])
if
bool
(
blog
):
blogs
.
append
(
blog
)
return
{
"blogs"
:
blogs
,
"resources"
:
validUrls
}
try
:
links
=
self
.
google
(
f
"site:medium.com {self.title} after:2020-01-01"
)
validUrls
=
self
.
getValidUrls
(
links
)
validSets
=
self
.
getValidSets
(
validUrls
)
blogs
=
[]
for
validset
in
validSets
:
blog
=
self
.
getBlogs
(
validset
[
"username"
],
validset
[
"tag"
])
if
bool
(
blog
):
blogs
.
append
(
blog
)
return
{
"blogs"
:
blogs
,
"resources"
:
validUrls
}
except
Exception
as
e
:
print
(
e
)
return
{
"blogs"
:
[],
"resources"
:
[]}
backend/python/auto-answer/github.py
View file @
0a8c44ae
...
...
@@ -67,11 +67,15 @@ class GithubData:
:param query: The search query.
:return: The repos found.
"""
google_query
=
"site:github.com {}"
.
format
(
query
)
search_args
=
(
google_query
,
1
)
gsearch
=
GoogleSearch
()
gresults
=
gsearch
.
search
(
*
search_args
)
return
gresults
[
"links"
]
try
:
google_query
=
"site:github.com {}"
.
format
(
query
)
search_args
=
(
google_query
,
1
)
gsearch
=
GoogleSearch
()
gresults
=
gsearch
.
search
(
*
search_args
)
return
gresults
[
"links"
]
except
Exception
as
e
:
print
(
e
)
return
[]
def
get_valid_urls
(
self
,
links
):
"""
...
...
@@ -80,7 +84,10 @@ class GithubData:
validUrls
=
[]
for
i
in
links
:
if
"github.com"
in
i
:
uriTrimmed
=
re
.
match
(
r"^.*?\&sa="
,
i
[
29
:])
.
group
(
0
)
ur
=
uriTrimmed
.
replace
(
"&sa="
,
""
)
validUrls
.
append
(
ur
)
try
:
uriTrimmed
=
re
.
match
(
r"^.*?\&sa="
,
i
[
29
:])
.
group
(
0
)
ur
=
uriTrimmed
.
replace
(
"&sa="
,
""
)
validUrls
.
append
(
ur
)
except
Exception
as
e
:
print
(
e
)
return
validUrls
\ No newline at end of file
backend/python/auto-answer/scrapper.py
View file @
0a8c44ae
...
...
@@ -30,18 +30,20 @@ def saveAnswer(ans_id, stackoverflow, videos, medium_r, dev_r, github_r):
}
},
)
except
NameError
as
err
:
print
(
"ERRORRR"
)
print
(
err
)
if
__name__
==
"__main__"
:
# title =
input("Enter question title: ")
# title =
"How to center a div verticly"
title
=
sys
.
argv
[
1
]
# "what are the benefits of using java for mobile app development over flutter"
tags
=
sys
.
argv
[
2
]
# ["flutter","java"]
AUTO_ANS_ID
=
sys
.
argv
[
3
]
# "611feaff2c4db730e56d78e8"
tags
=
sys
.
argv
[
2
]
# tags = ["flutter", "java"]
AUTO_ANS_ID
=
sys
.
argv
[
3
]
# AUTO_ANS_ID = "60de81bf85684c45442aba3d"
stack
=
STOF
(
title
)
medium
=
Medium
(
title
,
tags
)
...
...
backend/python/auto-answer/stof.py
View file @
0a8c44ae
...
...
@@ -2,6 +2,7 @@ import requests
from
bs4
import
BeautifulSoup
import
re
from
lxml
import
etree
from
search_engine_parser
import
GoogleSearch
class
STOF
:
...
...
@@ -12,6 +13,13 @@ class STOF:
self
.
urls
=
[]
def
searchQuestion
(
self
):
# query = f"site:stackoverflow.com {self.title}"
# search_args = (query, 1)
# gsearch = GoogleSearch()
# gresults = gsearch.search(*search_args)
# for result in gresults["links"]:
# return gresults["links"]
html_page
=
requests
.
get
(
f
"https://google.com/search?q=site
%3
Astackoverflow.com+{self.qtitle}"
)
...
...
@@ -38,12 +46,14 @@ class STOF:
answers_count
=
dom
.
xpath
(
'//*[@id="answers-header"]/div/div[1]/h2'
)[
0
]
.
text
.
strip
()
print
(
"answers_count"
)
print
(
answers_count
)
answer
=
{
"url"
:
url
}
if
answers_count
!=
""
:
try
:
verified_answer
=
dom
.
xpath
(
'//*[@class="answer accepted-answer"]/div/div[2]/div[1]'
'//*[@class="answer
js-answer
accepted-answer"]/div/div[2]/div[1]'
)[
0
]
answer
[
"content"
]
=
etree
.
tostring
(
verified_answer
)
.
decode
(
"utf-8"
)
answer
[
"status"
]
=
"Verified"
...
...
@@ -53,7 +63,7 @@ class STOF:
except
:
try
:
first_answer
=
dom
.
xpath
(
'//*[@class="answer"]/div/div[2]/div[1]'
)[
first_answer
=
dom
.
xpath
(
'//*[@class="answer
js-answer
"]/div/div[2]/div[1]'
)[
0
]
answer
[
"content"
]
=
etree
.
tostring
(
first_answer
)
.
decode
(
"utf-8"
)
...
...
@@ -81,6 +91,6 @@ class STOF:
def
get_clean_url
(
url
):
import
re
pattern
=
r"^.*?\&sa="
return
re
.
match
(
pattern
,
url
)
.
group
(
0
)
backend/python/auto-answer/youtube.py
View file @
0a8c44ae
...
...
@@ -27,4 +27,4 @@ class Youtube:
videos
.
append
(
i
[
"link"
])
print
(
i
[
"link"
])
return
videos
\ No newline at end of file
backend/requirements.txt
View file @
0a8c44ae
bson==0.5.10
beautifulsoup4==4.9.3
dnspython==2.1.0
lxml==4.6.1
pymongo==3.11.4
regex==2020.7.14
requests==2.24.0
requests-html==0.10.0
scipy==1.5.4
search-engine-parser==0.6.2
youtube-search-python==1.4.6
beautifulsoup4>=4.9.1
dnspython>=2.1.0
lxml>=4.5.2
pymongo>=3.12.1
regex>=2020.7.14
requests>=2.24.0
requests-html>=0.10.0
scipy>=1.5.4
search-engine-parser>=0.6.2
youtube-search-python>=1.4.6
backend/routes.js
View file @
0a8c44ae
...
...
@@ -4,7 +4,12 @@ const {
authenticate
,
listUsers
,
search
,
find
find
,
createTelegramValidateCode
,
connectWithTelegram
,
verifyTelegramConnection
,
disconnectTelegram
,
verifyTelegramUsername
}
=
require
(
'
./controllers/users
'
);
const
{
loadQuestions
,
...
...
@@ -42,7 +47,11 @@ router.post('/authenticate', validateUser, authenticate);
router
.
get
(
'
/users
'
,
listUsers
);
router
.
get
(
'
/users/:search
'
,
search
);
router
.
get
(
'
/user/:username
'
,
find
);
router
.
post
(
'
/user/createtgcode
'
,
requireAuth
,
createTelegramValidateCode
);
router
.
post
(
'
/user/connect-telegram
'
,
connectWithTelegram
);
router
.
post
(
'
/user/verify-telegram-connection
'
,
verifyTelegramConnection
);
router
.
delete
(
'
/user/disconnect-telegram
'
,
disconnectTelegram
);
router
.
post
(
'
/user/verify-telegram-id
'
,
verifyTelegramUsername
);
//questions
router
.
param
(
'
question
'
,
loadQuestions
);
router
.
post
(
'
/questions
'
,
[
requireAuth
,
questionValidate
],
createQuestion
);
...
...
backend/utils/telegram.js
0 → 100644
View file @
0a8c44ae
const
create6DigitCode
=
()
=>
{
return
Math
.
floor
(
Math
.
random
()
*
900000
)
+
100000
;
};
module
.
exports
=
{
create6DigitCode
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a 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