-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_bot(test).py
More file actions
213 lines (180 loc) · 8.69 KB
/
final_bot(test).py
File metadata and controls
213 lines (180 loc) · 8.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from github import Github
from funcs import get_all_commits_by_user
from linkedin_api import Linkedin
import telebot
from PIL import Image
import docx
from docx.shared import Cm
import requests
import os
import re
# Read bot credentials from file
with open('/Users/monkey/Public/Python/Hidden files/LinkedIn_Bot.txt', 'r') as file:
key = file.readline().rstrip('\n')
mail = file.readline().rstrip('\n')
passwd = file.readline().rstrip('\n')
git_tok = file.readline().rstrip('\n')
# Initialize telebot
bot = telebot.TeleBot(key)
# Define button labels and payloads
start_button_label = "Start dossier"
menu_button_label = "Menu"
next_button_label = "Next"
start_btn_pl = "start_press"
menu_btn_pl = "menu_press"
next_btn_pl = "next_press"
# Define buttons
start_press = telebot.types.InlineKeyboardButton(start_button_label, callback_data=start_btn_pl)
menu_press = telebot.types.InlineKeyboardButton(menu_button_label, callback_data=menu_btn_pl)
next_press = telebot.types.InlineKeyboardButton(next_button_label, callback_data=next_btn_pl)
# Define keyboards
keyboard1 = telebot.types.InlineKeyboardMarkup(row_width=1)
keyboard1.add(start_press)
keyboard2 = telebot.types.InlineKeyboardMarkup(row_width=1)
keyboard2.add(menu_press)
keyboard4 = telebot.types.InlineKeyboardMarkup(row_width=1)
keyboard4.add(next_press)
# Send welcome message with keyboard1
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.send_message(message.chat.id, "Welcome to Stat Bot! Choose the site:", reply_markup=keyboard1)
# Handle button taps
@bot.callback_query_handler(func=lambda call: True)
def handle_button_tap(call):
if call.data == start_btn_pl:
bot.send_message(call.message.chat.id, "Please enter a valid GitHub link:", reply_markup=keyboard2)
bot.register_next_step_handler(call.message, get_github_link)
elif call.data == next_btn_pl:
bot.send_message(call.message.chat.id, "First part done! "
"\nPlease enter a valid LinkedIn link:", reply_markup=keyboard2)
bot.register_next_step_handler(call.message, get_linkedin_link)
elif call.data == menu_btn_pl:
bot.send_message(call.message.chat.id, "\tMenu!\n Choose the site:", reply_markup=keyboard1)
else:
bot.send_message(call.message.chat.id, "Unknown button tapped.")
bot.edit_message_reply_markup(chat_id=call.message.chat.id, message_id=call.message.message_id, reply_markup=None)
def get_github_link(message):
if message.text.startswith("https://github.com/") or message.text.startswith("github.com/"):
# Call Func
gitHubFill(message)
bot.send_message(message.chat.id, "Done, click NEXT button!", reply_markup=keyboard4)
else:
bot.send_message(message.chat.id, "Please enter a valid GitHub link starting "
"with 'https://github.com/' or 'github.com/'", reply_markup=keyboard2)
bot.register_next_step_handler(message, get_github_link)
def gitHubFill(message):
url = message.text
username = url[len('https://github.com/'):].split('/')[0]
print(username)
g = Github(git_tok)
# Func About Commits
get_all_commits_by_user(username)
user = g.get_user(username)
# Get user information
repos_num = user.public_repos
num_stars = user.get_starred().totalCount
num_followers = user.followers
num_following = user.following
# Get profile image
image_url = user.avatar_url
response = requests.get(image_url)
with open(f"Users_docs/profile_img_{message.chat.id}.png", "wb") as f:
f.write(response.content)
# Insert the image at the center of the first line
doc = docx.Document('Old_Docs/template.docx')
first_paragraph = doc.paragraphs[0]
run = first_paragraph.add_run()
img = Image.open(f'Users_docs/profile_img_{message.chat.id}.png')
img = img.convert('RGB')
img.save(f'Users_docs/profile_img_{message.chat.id}.png', 'PNG')
run.add_picture(f'Users_docs/profile_img_{message.chat.id}.png', width=Cm(4),
height=Cm(4))
run.alignment = 1 # Set the alignment to center
# Loop through all paragraphs in the document
for paragraph in doc.paragraphs:
if '<username>' in paragraph.text:
paragraph.text = paragraph.text.replace('<username>', username)
if '<repos_num>' in paragraph.text:
paragraph.text = paragraph.text.replace('<repos_num>', str(repos_num))
if '<stars_num>' in paragraph.text:
paragraph.text = paragraph.text.replace('<stars_num>', str(num_stars))
if '<followers_num>' in paragraph.text:
paragraph.text = paragraph.text.replace('<followers_num>', str(num_followers))
if '<following_num>' in paragraph.text:
paragraph.text = paragraph.text.replace('<following_num>', str(num_following))
# Insert the image at the end of the last line
last_paragraph = doc.paragraphs[-1]
run = last_paragraph.add_run()
run.add_picture(f'Users_docs/Im_Stat_{username}.png', width=Cm(18), height=Cm(8))
run.add_break()
run.alignment = 1 # Set the alignment to center
# Save the modified document
doc.save(f'Users_docs/dossier_{message.chat.id}.docx')
def get_linkedin_link(message):
linkedin_link = message.text
if linkedin_link.startswith("https://www.linkedin.com/") or linkedin_link.startswith("www.linkedin.com/"):
# Authenticate using bot account
api = Linkedin(mail, passwd)
username_pattern = r"in\/([^\?\/]*)"
match = re.search(username_pattern, linkedin_link)
if match:
username = match.group(1)
else:
username = None
# GET a profile
profile = api.get_profile(username)
first_name = profile['firstName']
last_name = profile['lastName']
employment = profile['headline']
location = profile['locationName'] + profile['geoLocationName']
if profile['summary']:
bio = profile['summary']
else:
bio = None
# Open the Word document
doc = docx.Document(f'Users_docs/dossier_{message.chat.id}.docx')
# Writing data
for paragraph in doc.paragraphs:
if '<first_name>' in paragraph.text:
paragraph.text = paragraph.text.replace('<first_name>', first_name)
if '<last_name>' in paragraph.text:
paragraph.text = paragraph.text.replace('<last_name>', last_name)
if '<employment>' in paragraph.text:
paragraph.text = paragraph.text.replace('<employment>', employment)
if '<bio>' in paragraph.text:
paragraph.text = paragraph.text.replace('<bio>', bio)
if '<followers_num>' in paragraph.text:
paragraph.text = paragraph.text.replace('<followers_num>', "followers_num")
if '<location>' in paragraph.text:
paragraph.text = paragraph.text.replace('<location>', location)
if '<skills>' in paragraph.text:
skills_list = profile['skills']
skills_str = ", ".join(skill['name'] for skill in skills_list)
paragraph.text = paragraph.text.replace('<skills>', skills_str)
if '<education>' in paragraph.text:
education_list = []
for edu in profile['education']:
if 'school' in edu:
school_name = edu['school']['schoolName']
education_list.append(school_name)
else:
print("No school information available")
# join the list of school names with commas
education_str = ", ".join(education_list)
# replace <education> in paragraph.text with the comma-separated string
paragraph.text = paragraph.text.replace('<education>', education_str)
# Save the modified document
doc.save(f'Users_docs/dossier_{message.chat.id}.docx')
# Sending a file
bot.send_document(chat_id=message.chat.id,
document=open(
f'/Users/monkey/Public/Python/Python_Bot/Users_docs/dossier_{message.chat.id}.docx',
'rb'))
os.system("touch " + "/Users/monkey/Public/Python/Python_Bot/Users_docs")
bot.send_message(message.chat.id, "Start new :)", reply_markup=keyboard1)
else:
bot.send_message(message.chat.id, "Please enter a valid LinkedIn link starting with 'https://www.linkedin.com/'"
" or 'www.linkedin.com/'", reply_markup=keyboard2)
bot.register_next_step_handler(message, get_linkedin_link)
if __name__ == '__main__':
bot.polling()