Use debian 2.7 only
[fpbd-bostik.git] / pyfpdb / ScriptFetchMergeResults.py
blob07ac1e014dd37b6d8ab82caea43c55f46ea8b1af
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #Copyright 2008-2011 Carl Gherardi
5 #This program is free software: you can redistribute it and/or modify
6 #it under the terms of the GNU Affero General Public License as published by
7 #the Free Software Foundation, version 3 of the License.
9 #This program is distributed in the hope that it will be useful,
10 #but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 #GNU General Public License for more details.
14 #You should have received a copy of the GNU Affero General Public License
15 #along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #In the "official" distribution you can find the license in agpl-3.0.txt
18 """A script for fetching Winamax tourney results"""
19 import L10n
20 _ = L10n.get_translation()
22 import Configuration
23 import Database
25 import logging, os, sys
26 import re, urllib2
29 def fetch_results_page(tourney_id):
30 url = "http://www.carbonpoker.ag/tournaments/tournament-details.html?id=%s-1" % tourney_id
31 try:
32 data = urllib2.urlopen(url).read()
33 return data
34 except urllib2.HTTPError, e:
35 print e, tourney_id
36 return None
38 def write_file(filename, data):
39 f = open(filename, 'w')
40 f.write(data)
41 f.close()
43 def main():
44 Configuration.set_logfile("fpdb-log.txt")
45 config = Configuration.Config()
46 db = Database.Database(config)
48 tourney_ids = db.getSiteTourneyNos("Merge")
49 tids = []
51 for tid in tourney_ids:
52 blah, = tid # Unpack tuple
53 tids.append(str(blah))
54 # winamax_get_winning(tid,"blah")
55 results_dir = config.get_import_parameters().get("ResultsDirectory")
56 results_dir = os.path.expanduser(results_dir)
57 site_dir = os.path.join(results_dir, "Merge")
58 print "DEBUG: site_dir: %s" % site_dir
59 filelist = [file for file in os.listdir(site_dir) if not file in [".",".."]]
60 print "DEBUG: filelist : %s" % filelist
61 print "DEBUG: tids : %s" % tids
63 for f in filelist:
64 try:
65 tids.remove(f)
66 except ValueError:
67 print "Warning: '%s' is not a known tourney_id" % f
69 if len(tids) == 0:
70 print "No tourney results files to fetch"
71 else:
72 for tid in tids:
73 filename = os.path.join(site_dir, tid)
74 data = fetch_results_page(tid)
75 if data != None:
76 print u"DEBUG: write_file(%s)" %(filename)
77 write_file(filename, data)
79 if __name__ == '__main__':
80 main()