-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsak4dg.py
94 lines (85 loc) · 2.99 KB
/
sak4dg.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
import urllib.request
import urllib.parse
import http.cookiejar
import re
import sys
# global administrivia
__version__ = "sak4dg v0.01"
__copyright__ = """\
Copyright (C) 2011 Maurice "Mucki" Kemmann <[email protected]>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."""
### Init ###
BASE_URL = "http://dailygammon.com"
# Please fill in your data
USERID = "your_userid"
MATCH_DIR = "/your/path/to/saved/matches/" #Target path for your matches, incl. "/" at the end
USERNAME = "your_username"
PASSWORD = "your_password" #clear text, not encoded
def check_python_version():
"""Abort if we are running on python < v3.1"""
too_old_error = "This program requires python v3.1 or greater. " + \
"Your version of python is:\n%s""" % sys.version
try:
version = sys.version_info # we might not even have this function! :)
if (version[0] < 3) or (version[0] == 3 and version[1] < 1):
print(too_old_error)
sys.exit(1)
except AttributeError:
print(too_old_error)
sys.exit(1)
def site_login(url, username, password):
"""Login and cookie handling"""
cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
login_data = urllib.parse.urlencode({'login' : username, 'password' : password})
login_data = login_data.encode("utf-8")
opener.open(BASE_URL+"/bg/login", login_data)
return opener
def get_urls():
"""gets all urls of your matches on Dailygammon"""
opener = site_login(BASE_URL, USERNAME, PASSWORD)
url = BASE_URL + "/bg/user/" + USERID + "?finished=1"
resp = opener.open(url)
result = resp.read()
txt = result.decode("iso-8859-1")
liste = (re.findall("/bg/export/[\d]{4,8}",txt))
return liste
def save_match(url, match):
"""Saves a match"""
p = re.compile("[\d]{4,8}")
filename = p.search(url)
filename = filename.group() + ".mat"
f = open(MATCH_DIR + filename, "w")
f.write(match)
f.close()
print("\nMatch " + filename + " geschrieben.")
def FileExist(file):
""" Checks if a file exists or not. """
try:
oFile = open(file,"r")
except IOError: #EA-Fehler
return 0
else:
oFile.close()
return 1
def get_matches(list_of_urls):
"""Get only matches not already saved"""
p = re.compile("[\d]{4,8}")
opener = site_login(BASE_URL, USERNAME, PASSWORD)
j = len(list_of_urls)
for i in range(1,j):
filename = p.search(list_of_urls[i])
filename = filename.group() + ".mat"
path = MATCH_DIR + filename
if FileExist(path) == 0:
match_url = BASE_URL + list_of_urls[i]
resp = opener.open(match_url)
match = resp.read()
match = match.decode("iso-8859-1")
save_match(list_of_urls[i], match)
else:
print(".")
check_python_version()
urls = get_urls()
get_matches(urls)