-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneral.py
70 lines (55 loc) · 2.47 KB
/
general.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
import re
import browser_cookie3 as bc
# this dictionary holds language name as key and corresponding ISO language code as value
LANG_NAME_TO_CODE_MAPPING = {'Arabic': 'ar', 'Afrikaans': 'af',
'Bangla': 'bn', 'Burmese': 'my',
'Chinese (Simplified)': 'zh-Hans', 'Chinese (Traditional)': 'zh-Hant', 'Chinese': 'zh',
'Dutch': 'nl',
'English': 'en',
'French': 'fr', 'Finnish': 'fi',
'Greek': 'el',
'Hindi': 'hi',
'Italian': 'it',
'Japanese': 'ja',
'Korean': 'ko',
'Malay': 'ml', 'Malayalam': 'ml',
'Portugese': 'pt',
'Russian': 'ru',
'Spanish': 'es',
'Tamil': 'ta', 'Telegu': 'te', 'Thai': 'th', 'Turkish': 'tr',
'Urdu': 'ur',
'Vietnamese': 'vi',
'-ALL AVAILABLE': 'all', '-NONE': ''}
# extract class name from course home page url
def urltoclassname(homepageurl):
'''this function assumes that the url is of this format:
coursera.org/learn/CLASSNAME/more thing...
if the url isn't in this format, program won't work'''
classname = ''
if ('/' in homepageurl) or ('\\' in homepageurl):
classname = re.findall(
'coursera.org/learn/(.+?)/', homepageurl.lower())
classname = ''.join(classname) # convert list to string
else:
# if homepageurl doesn't contain slash treat it as just a string
# 'python-network-data' would output 'python-network-data'
classname = homepageurl
return classname
def loadcauth(domain):
'''this function returns the cauth code of browser for the specified domain.
example use: loadcauth('coursera.org'). the function searches only in the cookie
files of chrome and firefox. if there is no cauth for the domain function returns
an empty string'''
cj = bc.load(domain_name=domain)
strcookie = str(cj)
cauth = re.findall('CAUTH=(.*?)\s', strcookie)
# print(strcookie)
# print(len(cauth))
if len(cauth) > 0:
cauth = cauth[0]
else:
cauth = ''
return cauth
# print(loadcauth('coursera.org'))
# url = r"python"
# print(urltoclassname(url))