-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
655 lines (546 loc) · 22.5 KB
/
helpers.py
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
#!/usr/bin/python
from datetime import date, datetime
from thefuzz import fuzz
#not using older fuzzywuzzy
from itertools import combinations
from lxml import html as lxml_html
import json
import pandas as pd
import re
import requests
import time
import unidecode
import urllib.parse
import math
from py_stringmatching import utils
from py_stringmatching import Levenshtein, GeneralizedJaccard,Jaro,JaroWinkler
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
import logging
import logging.handlers
import configparser
from random import randint
import os
import numpy as np
import copy
import psutil
from lxml.html.soupparser import fromstring as soup_fromstring
service_list = ['amazon','apple','gaana','hungama','jiosaavn','spotify','wynk','ytmusic']
def time_sleep(n1,n2):
'''
makes program sleep for n seconds.
n being any number from n1 to n2
'''
delay = randint(n1,n2)
time.sleep(delay)
def tokenize(keywords):
'''
Takes a string of keywords, lower-cases every word,
removes punctuation, ascii-fies all the letters,
so no diacritics etc.
Returns a python list of all these words/tokens.
Also removes 'various' and 'artists' from this list of tokens
'''
if (keywords == '') or (isinstance(keywords, float) and math.isnan(keywords)):
return []
keywords = keywords.lower()
keywords = keywords.replace("‘","'").replace("’","'")
# line below is for removing punctuation, keeps single apostrophes and periods in
keywords = re.sub(r"[^\w\s.']", ' ', keywords)
keywords = unidecode.unidecode(keywords)
# dont have to worry about trimming leading and trailing space or
# multiple spaces in a string. When you split, all that is taken care of
keywords_list = keywords.split()
# keeping in any keyword with 2 characters or more
# NOPE. decided to keep in single character keywords too
# keywords_list = [keywordx for keywordx in keywords_list
# if len(keywordx) >= 2]
# sort list by keyword length, longest keyword comes first
for tokenx in ['various','artists']:
while tokenx in keywords_list:
keywords_list.remove(tokenx)
return keywords_list
def create_list_queries(title,artist):
'''
Takes title of album and name of artists, creates a list of queries.
The title & artist name are transformed, they're ascii-fied, lowercased etc.
This then becomes a list of keywords.
The function returns a list of combinations of these keywords.
The first combination will have all the keywords.
The combinations after that will have all the keywords but one, and so on
This is fed into a service one after the other to find a specific album.
'''
keywords = artist + ' ' + title
keywords_list = tokenize(keywords)
string_combos = [keywords_list]
keywords_list_sorted = sorted(keywords_list, key=len,reverse=True)
len_keywords_list_sorted = len(keywords_list_sorted)
#if no result at first, use combinations of keywords
#these combinations come after query containing all keywords
#a combination will have at least 3 keywords
if len_keywords_list_sorted >= 4:
string_combos_additional = list(combinations(keywords_list_sorted,
(len_keywords_list_sorted - 1)))
string_combos.extend(string_combos_additional)
if len_keywords_list_sorted >= 5:
string_combos_additional_2 = \
list(combinations(keywords_list_sorted,
(len_keywords_list_sorted - 2)))
string_combos.extend(string_combos_additional_2)
# DO I NEED THE SET OF COMBOS BELOW? CREATES TOO MANY QUERIES TO GO THROUGH
# i think once we reach this point, we know a particular album isnt there.
if len_keywords_list_sorted >= 6:
string_combos_additional_3 =\
list(combinations(keywords_list_sorted,
(len_keywords_list_sorted - 3)))
string_combos.extend(string_combos_additional_3)
return string_combos
def get_elkan_score(source_string, target_string):
'''
Takes a source string and target string, creates two bags (python lists) of
tokens from them, and returns a value from 0-100 for how similar they are.
The higher the value, the more similar.
Here I'm just modifying the code from the py_stringmatching package.
Specifically the code from their implementation of the monge-elkan measure.
https://github.com/anhaidgroup/py_stringmatching/blob/master/py_stringmatching/similarity_measure/monge_elkan.py
'''
lev = Levenshtein()
bag1 = tokenize(source_string)
bag2 = tokenize(target_string)
# input validations
utils.sim_check_for_none(bag1, bag2)
utils.sim_check_for_list_or_set_inputs(bag1, bag2)
# if exact match return 100
if utils.sim_check_for_exact_match(bag1, bag2):
return 100
# if one of the strings is empty return 0
if utils.sim_check_for_empty(bag1, bag2):
return 0
# aggregated sum of all the max sim score of all the elements in bag1
# with elements in bag2
sum_of_maxes = 0
for el1 in bag1:
max_sim = float('-inf')
for el2 in bag2:
max_sim = max(max_sim, lev.get_sim_score(el1, el2))
sum_of_maxes += max_sim
elkan_score = (float(sum_of_maxes) / float(len(bag1)))*100
return elkan_score
def get_elkan_score_jaro(source_string, target_string):
jaro = Jaro()
bag1 = tokenize(source_string)
bag2 = tokenize(target_string)
# input validations
utils.sim_check_for_none(bag1, bag2)
utils.sim_check_for_list_or_set_inputs(bag1, bag2)
# if exact match return 100
if utils.sim_check_for_exact_match(bag1, bag2):
return 100
# if one of the strings is empty return 0
if utils.sim_check_for_empty(bag1, bag2):
return 0
# aggregated sum of all the max sim score of all the elements in bag1
# with elements in bag2
sum_of_maxes = 0
for el1 in bag1:
max_sim = float('-inf')
for el2 in bag2:
max_sim = max(max_sim, jaro.get_sim_score(el1, el2))
sum_of_maxes += max_sim
elkan_score = (float(sum_of_maxes) / float(len(bag1)))*100
return elkan_score
def get_elkan_score_jarowink(source_string, target_string):
'''
Takes a source string and target string, creates two bags (python lists) of
tokens from them, and returns a value from 0-100 for how similar they are.
The higher the value, the more similar.
Here I'm just modifying the code from the py_stringmatching package.
Specifically the code from their implementation of the monge-elkan measure.
https://github.com/anhaidgroup/py_stringmatching/blob/master/py_stringmatching/similarity_measure/monge_elkan.py
Here the sim_func is jarowink instead of levenshtein as in get_elkan_score
'''
jarowink = JaroWinkler()
bag1 = tokenize(source_string)
bag2 = tokenize(target_string)
# input validations
utils.sim_check_for_none(bag1, bag2)
utils.sim_check_for_list_or_set_inputs(bag1, bag2)
# if exact match return 100
if utils.sim_check_for_exact_match(bag1, bag2):
return 100
# if one of the strings is empty return 0
if utils.sim_check_for_empty(bag1, bag2):
return 0
# aggregated sum of all the max sim score of all the elements in bag1
# with elements in bag2
sum_of_maxes = 0
for el1 in bag1:
max_sim = float('-inf')
for el2 in bag2:
max_sim = max(max_sim, jarowink.get_sim_score(el1, el2))
sum_of_maxes += max_sim
elkan_score = (float(sum_of_maxes) / float(len(bag1)))*100
return elkan_score
def create_session():
# code below from https://stackoverflow.com/questions/47675138/how-to-override-backoff-max-while-working-with-requests-retry/
class RetryRequest(Retry):
def __init__(self, backoff_max=Retry.BACKOFF_MAX, **kwargs):
super().__init__(**kwargs)
self.BACKOFF_MAX = backoff_max
def new(self, **kwargs):
return super().new(backoff_max=self.BACKOFF_MAX, **kwargs)
# code below from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
retry_strategy = RetryRequest(
total = 3,
read = 3,
connect = 3,
status = 3,
redirect = 3,
backoff_factor = 2,
backoff_max = 7200,
status_forcelist=[101, 104, 301, 404, 408, 413, 429, 500, 502, 503, 504],
)
DEFAULT_TIMEOUT = (5, 5) # tuple for connect and read timeout in seconds
class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
self.timeout = DEFAULT_TIMEOUT
if "timeout" in kwargs:
self.timeout = kwargs["timeout"]
del kwargs["timeout"]
super().__init__(*args, **kwargs)
def send(self, request, **kwargs):
timeout = kwargs.get("timeout")
if timeout is None:
kwargs["timeout"] = self.timeout
return super().send(request, **kwargs)
adapter = TimeoutHTTPAdapter(max_retries=retry_strategy, timeout=(10, 20))
session = requests.Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
return session
def create_logger():
'''
creates a logger that extracts any error message and sends it in an email
'''
logger = logging.getLogger()
# below is just code to send me an email if the script run fails for any reason
config_email = configparser.ConfigParser()
config_email.read('creds_headers/config_email.ini')
receiver_email = config_email['info']['email']
sender_email = config_email['info']['sender_email']
sender_password = config_email['info']['sender_password']
time_now = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
subjectx = time_now + " Error: music service search"
smtp_handler = logging.handlers.SMTPHandler(
mailhost=("smtp.gmail.com", 587),
fromaddr=sender_email,
toaddrs=receiver_email,
subject=subjectx,
credentials=(sender_email, sender_password),
secure=()
)
logger.addHandler(smtp_handler)
return logger
def check_subset(string1,string2):
'''
Takes two strings string1 and string2\n
Checks if the words from string1 are in string2 or vice versa\n
That is, if either is a subset of the other.\n
It does this by tokenizing the two strings into python lists\n
Uses set operations to see if all elements from one list are in the other\n
The words will exclude 'various' and 'artists'\n
Returns a boolean value True or False\n
'''
string1_list = tokenize(string1)
string2_list = tokenize(string2)
if((set(string1_list).issubset(set(string2_list))) or
(set(string2_list).issubset(set(string1_list)))):
return True
else:
return False
def get_genjacc_score(source_string, target_string):
'''
Takes a source string and target string, creates two bags (python lists) of
tokens from them, and returns a value from 0-100 for how similar they are.
The higher the value, the more similar.
Here i'm using something called generalized jaccard algorithm. The more words
or tokens that the two strings DON'T have in common, the lower the score.
So this is different from elkan_score which was just looking at 'subset-iness'
'''
bag1 = tokenize(source_string)
bag2_raw = tokenize(target_string)
redundant_words_list = ["edition","deluxe","version","anniversary",
"remastered","remaster","expanded","bonus","track","international",
"reissue","explicit","collector's","extended","ultimate","special",
"legacy","studio","essential","original","supreme","complete","super",
"tracks","re-issue",]
bag2 = [x for x in bag2_raw if x not in redundant_words_list]
gj = GeneralizedJaccard(sim_func=Levenshtein().get_sim_score, threshold=0.0)
genjacc_score = gj.get_sim_score(bag1, bag2) * 100
return genjacc_score
def get_genjacc_score_jarowink(source_string, target_string):
'''
Takes a source string and target string, creates two bags (python lists) of
tokens from them, and returns a value from 0-100 for how similar they are.
The higher the value, the more similar.
Here i'm using something called generalized jaccard algorithm. The more words
or tokens that the two strings DON'T have in common, the lower the score.
So this is different from elkan_score which was just looking at 'subset-iness'
Here the sim_func is jarowink instead of levenshtein
'''
bag1 = tokenize(source_string)
bag2_raw = tokenize(target_string)
redundant_words_list = ["edition","deluxe","version","anniversary",
"remastered","remaster","expanded","bonus","track","international",
"reissue","explicit","collector's","extended","ultimate","special",
"legacy","studio","essential","original","supreme","complete","super",
"tracks","re-issue",]
bag2 = [x for x in bag2_raw if x not in redundant_words_list]
gj = GeneralizedJaccard(sim_func=JaroWinkler().get_sim_score, threshold=0.0)
genjacc_score = gj.get_sim_score(bag1, bag2) * 100
return genjacc_score
def merge_round_csvs(service_list):
'''
Merges csvs from round 1 with those from round 2.
In round 1, we tried to get matches for all albums
In round 2, we focused on the albums without any matches in round 1
The merged CSVs are saved as matches_<service>_round_02_merged.csv
'''
for service in service_list:
round_01_csv_path = 'data/round_01/matches_' + service + '_round_01.csv'
round_01_df = pd.read_csv(round_01_csv_path, dtype = str,
index_col='index')
title_col_name = 'match_title_' + service + '_1'
round_01_df_matched = round_01_df[round_01_df[title_col_name].notnull()]
round_02_culled_csv_path = 'data/round_02/matches_' + service +\
'_round_02_culled.csv'
round_02_culled_df = pd.read_csv(round_02_culled_csv_path, dtype = str,
index_col='index')
round_02_merged_df = pd.concat([round_01_df_matched,
round_02_culled_df],
axis=0)
round_02_merged_csv_path = 'data/round_02/matches_' + service +\
'_round_02_merged.csv'
round_02_merged_df.to_csv(round_02_merged_csv_path, encoding='utf-8')
def merge_service_csvs():
'''
Takes CSVs with the suffix round_nn from folder round_nn and merges them.
Each CSV is the result of a search for matches on a particular music service
All these CSVs are merged with the all_data file
Then saves everthing as merged_csv_name
'''
all_data_csv_path = 'data/all_data_v09.csv'
albums_df = pd.read_csv(all_data_csv_path, dtype = str, index_col='index')
for service in service_list:
service_csv_path = 'data/round_02/matches_' + service + '_' +\
'round_02_merged.csv'
service_df = pd.read_csv(service_csv_path, dtype = str,
index_col='index')
service_df.drop(labels=['year','title','artist','source','canon_contemp',
'genre','list_name'], axis=1, inplace=True)
albums_df = albums_df.join(service_df)
merged_csv_path = 'data/round_02_all.csv'
albums_df.to_csv(merged_csv_path, encoding='utf-8')
def insert_genjacc_score_columns():
'''
Makes a dataframe out of merged_csv_name\n
Goes through each service, and each match\n
Finds out what the generalized jaccard score is for the match's artist and
title combined\n
Inserts a column with that score into the dataframe\n
Then saves everything as csv_with_scores_name
'''
# NEED TO EXCLUDE WORDS LIkE DELUXE, REMASTER, REISSUE ETC.
merged_csv_path = 'data/round_02_all.csv'
merged_df = pd.read_csv(merged_csv_path, dtype = str,
index_col='index')
for index,row in merged_df.iterrows():
source_title = row['title']
source_artist = row['artist']
source_artist_title_combined = source_artist + source_title
for service in service_list:
for match_num in ['1','2','3']:
target_artist_col_name = 'match_artist_' + service + '_' +\
match_num
target_title_col_name = 'match_title_' + service + '_' +\
match_num
target_artist = row[target_artist_col_name]
target_title = row[target_title_col_name]
#if a NaN value, break loop
if ((isinstance(target_title, float) and math.isnan(target_title)) or\
(isinstance(target_artist, float) and math.isnan(target_artist))):
break
target_artist_title_combined = target_artist + target_title
#below we get the similiarity score acc to an algo called
# generalized jaccard
score_raw = get_genjacc_score_jarowink(source_artist_title_combined,
target_artist_title_combined)
score = round(score_raw,1)
score_col_name = 'genjacc_score_' + service + '_match_' + str(match_num)
merged_df.loc[index, score_col_name] = score
csv_with_scores_path = 'data/round_02_all_with_genjacc_scores.csv'
merged_df.to_csv(csv_with_scores_path, encoding='utf-8')
def insert_tokenset_score_columns():
'''
Makes a dataframe out of merged_csv_name\n
Goes through each service, and each match\n
Finds out what the fuzzywuzzy token set score is for the match's artist and
title combined\n
Inserts a column with that score into the dataframe\n
Then saves everything as csv_with_scores_name
'''
merged_csv_path = 'data/round_02_all_with_genjacc_scores.csv'
merged_df = pd.read_csv(merged_csv_path, dtype = str,
index_col='index')
for index,row in merged_df.iterrows():
source_title = row['title']
source_artist = row['artist']
source_artist_title_combined = source_artist + source_title
for service in service_list:
for match_num in ['1','2','3']:
target_artist_col_name = 'match_artist_' + service + '_' +\
match_num
target_title_col_name = 'match_title_' + service + '_' +\
match_num
target_artist = row[target_artist_col_name]
target_title = row[target_title_col_name]
if ((isinstance(target_title, float) and math.isnan(target_title)) or\
(isinstance(target_artist, float) and math.isnan(target_artist))):
break
target_artist_title_combined = target_artist + target_title
#below we get the similiarity score acc to a
# method in fuzzywuzzy -- token_set_ratio
score_raw = fuzz.token_set_ratio(source_artist_title_combined,
target_artist_title_combined)
score = round(score_raw,1)
score_col_name = 'tokenset_score_' + service + '_match_' + str(match_num)
merged_df.loc[index, score_col_name] = score
csv_with_scores_path = 'data/round_02_all_with_tokenset_scores.csv'
merged_df.to_csv(csv_with_scores_path, encoding='utf-8')
def create_txt_false_positive_check(print_filename):
print_filepath = 'data/' + print_filename
if os.path.exists(print_filepath):
os.remove(print_filepath)
f = open(print_filepath,'a')
service_list = ['amazon','apple','gaana',
'hungama',
'jiosaavn','spotify','wynk','ytmusic']
scores_csv_path = 'data/round_02_all_with_tokenset_scores.csv'
scores_df = pd.read_csv(scores_csv_path, dtype = str, index_col='index')
for index,row in scores_df.iterrows():
source_artist = row['artist']
source_title = row['title']
statement_A = '\n' +\
'INDEX_NUMBER: ' + str(index) + '\n' +\
'SOURCE_TITLE: ' + source_title + ' * SOURCE_ARTIST: ' + source_artist
print(statement_A, file=f)
for service in service_list:
statement_B = '\n' +\
' SERVICE: ' + service
print(statement_B, file=f)
for match_num in ['1','2','3']:
target_title_col_name = 'match_title_' + service + '_' +\
match_num
target_artist_col_name = 'match_artist_' + service + '_' +\
match_num
target_title = row[target_title_col_name]
target_artist = row[target_artist_col_name]
if ((isinstance(target_title, float) and math.isnan(target_title)) or\
(isinstance(target_artist, float) and math.isnan(target_artist))):
break
# target_title = 'nan'
# target_artist = 'nan'
genjacc_score_col_name = 'genjacc_score_' + service + '_match_' + match_num
tokenset_score_col_name = 'tokenset_score_' + service + '_match_' + match_num
genjacc_score = row[genjacc_score_col_name]
tokenset_score = row[tokenset_score_col_name]
statement_C = '\n' +\
' MATCH_NUM: ' + match_num + '\n' +\
' TARGET_TITLE: ' + target_title + ' * TARGET_ARTIST: ' + target_artist + '\n' +\
' GENJACC_SCORE: ' + str(genjacc_score) + ' * TOKENSET_SCORE: ' + str(tokenset_score)
print(statement_C, file=f)
def create_csvs_manual_correction():
scores_csv_path = 'data/round_02_all_with_tokenset_scores.csv'
scores_df = pd.read_csv(scores_csv_path, dtype = str, index_col='index')
df_for_saving_all_columns = copy.deepcopy(scores_df)
df_for_saving = df_for_saving_all_columns[['title', 'artist']]
for index,row in scores_df.iterrows():
source_artist = row['artist']
source_title = row['title']
for service in service_list:
highest_genjacc_score = 0
df_for_saving.loc[index, service] = np.nan
for match_num in ['1','2','3']:
target_title_col_name = 'match_title_' + service + '_' +\
match_num
target_artist_col_name = 'match_artist_' + service + '_' +\
match_num
target_title = row[target_title_col_name]
target_artist = row[target_artist_col_name]
if ((isinstance(target_title, float) and math.isnan(target_title)) or\
(isinstance(target_artist, float) and math.isnan(target_artist))):
break
genjacc_score_col_name = 'genjacc_score_' + service + '_match_' + match_num
# tokenset_score_col_name = 'tokenset_score_' + service + '_match_' + match_num
genjacc_score_raw = row[genjacc_score_col_name]
genjacc_score = float(genjacc_score_raw)
# tokenset_score_raw = row[tokenset_score_col_name]
# tokenset_score = float(tokenset_score_raw)
if genjacc_score > highest_genjacc_score:
highest_genjacc_score = genjacc_score
df_for_saving.loc[index, service] = match_num
filepath_original = 'data/matches_original.csv'
df_for_saving.to_csv(filepath_original, encoding='utf-8')
filepath_corrected = 'data/matches_corrected.csv'
df_for_saving.to_csv(filepath_corrected, encoding='utf-8')
def create_session_hungama():
# code below from https://stackoverflow.com/questions/47675138/how-to-override-backoff-max-while-working-with-requests-retry/
class RetryRequest(Retry):
def __init__(self, backoff_max=Retry.BACKOFF_MAX, **kwargs):
super().__init__(**kwargs)
self.BACKOFF_MAX = backoff_max
def new(self, **kwargs):
return super().new(backoff_max=self.BACKOFF_MAX, **kwargs)
# code below from https://findwork.dev/blog/advanced-usage-python-requests-timeouts-retries-hooks/
retry_strategy = RetryRequest(
total = 3,
read = 3,
connect = 3,
status = 3,
redirect = 3,
backoff_factor = 2,
backoff_max = 7200,
status_forcelist=[101, 104, 301, 404, 408, 413, 429, 500, 502, 503, 504],
)
DEFAULT_TIMEOUT = 10
class TimeoutHTTPAdapter(HTTPAdapter):
def __init__(self, *args, **kwargs):
self.timeout = DEFAULT_TIMEOUT
if "timeout" in kwargs:
self.timeout = kwargs["timeout"]
del kwargs["timeout"]
super().__init__(*args, **kwargs)
def send(self, request, **kwargs):
timeout = kwargs.get("timeout")
if timeout is None:
kwargs["timeout"] = self.timeout
return super().send(request, **kwargs)
adapter = TimeoutHTTPAdapter(max_retries=retry_strategy, timeout=10)
session = requests.Session()
session.mount("https://", adapter)
session.mount("http://", adapter)
return session
# code from https://stackoverflow.com/a/63738197/6403539
def get_pids_by_script_name(script_name):
pids = []
for proc in psutil.process_iter():
try:
cmdline = proc.cmdline()
pid = proc.pid
except psutil.NoSuchProcess:
continue
if ((len(cmdline) >= 2)
and ('python3' in cmdline[0])
and (script_name in cmdline[1])):
pids.append(pid)
return pids