Changeset 8255535
- Timestamp:
- 06/28/10 19:28:01 (3 years ago)
- Branches:
- master
- Children:
- a5c46b2
- Parents:
- 8491ac9
- git-author:
- Aurélien Bompard <aurelien@…> (06/28/10 19:28:01)
- git-committer:
- Aurélien Bompard <aurelien@…> (06/28/10 19:28:01)
- File:
-
- 1 edited
-
make-songs-list.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
make-songs-list.py
r6fdcac4 r8255535 18 18 import optparse 19 19 import atexit 20 import re 20 21 from pprint import pprint 22 23 import PIL 21 24 22 25 from reportlab.platypus import BaseDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, Frame, PageTemplate … … 27 30 28 31 29 30 32 defaultPageSize = (defaultPageSize[1], defaultPageSize[0]) 31 33 PAGE_HEIGHT=defaultPageSize[1] 32 34 PAGE_WIDTH=defaultPageSize[0] 33 MARGIN = 835 MARGIN = 10 34 36 COLSEP = 5 35 37 36 38 songs = [] 37 39 38 def get_songs( ):40 def get_songs(cover_size, dpi): 39 41 songs_dirs = ["/usr/share/performous/songs", 40 42 "/usr/local/share/games/ultrastar/songs", … … 45 47 "~/.fretsonfire/songs", 46 48 ] 49 available_songs = [] 47 50 for songs_dir in songs_dirs: 48 51 songs_dir = os.path.expanduser(songs_dir) … … 53 56 if not cur_file.lower().endswith(".txt"): 54 57 continue 55 filepath = os.path.join(root, cur_file) 56 song = process_file(filepath) 57 if song: 58 songs.append(song) 58 available_songs.append(os.path.join(root, cur_file)) 59 for index, available_song in enumerate(available_songs): 60 song = process_file(available_song, cover_size, dpi) 61 if song: 62 songs.append(song) 63 sys.stdout.write("\rAdding song %d/%d" 64 % (index+1, len(available_songs))) 65 sys.stdout.flush() 66 print 67 print "Building PDF..." 59 68 songs.sort(cmp=lambda x,y: cmp(x["artist"],y["artist"])) 60 69 61 def process_file(filepath ):70 def process_file(filepath, cover_size, dpi): 62 71 song = {} 63 72 file_h = open(filepath, "r") … … 72 81 if tag not in song: 73 82 continue 74 song[ tag] = os.path.realpath(os.path.join(83 song["image"] = os.path.realpath(os.path.join( 75 84 os.path.dirname(filepath), song[tag])) 76 if song[tag].count(" "): 77 tmpimg, tmpimg_path = tempfile.mkstemp( 78 prefix="mksonglist-", suffix=".jpg") 79 print >>sys.stderr, "Warning on image '%s': " % song[tag] \ 80 +"the filename contains two consecutive spaces, " \ 81 +"ReportLab does not like that. Using a workaround." 82 origimg = open(song[tag]) 83 os.write(tmpimg, origimg.read()) 84 origimg.close() 85 atexit.register(os.remove, tmpimg_path) 86 song[tag] = tmpimg_path 87 if not os.path.exists(song[tag]): 88 del song[tag] 85 if not os.path.exists(song["image"]): 86 del song["image"] 87 continue 88 # Resize the image 89 tmpimg, tmpimg_path = tempfile.mkstemp( 90 prefix="mksonglist-", suffix=".jpg") 91 img = PIL.Image.open(song["image"]) 92 img.thumbnail((cover_size * dpi, cover_size * dpi), PIL.Image.ANTIALIAS) 93 img.save(os.fdopen(tmpimg, "w"), "JPEG") 94 atexit.register(os.remove, tmpimg_path) 95 song["image"] = tmpimg_path 96 break 89 97 return song 90 98 91 99 def filter_songs(karaoke): 100 filter_re = re.compile("(.*)\s+\(.*\)") 92 101 for song in songs[:]: 93 if not karaoke and song["title"].count("(Karaoke)"): 102 title = song["title"] 103 if not karaoke and title.count("(Karaoke)"): 94 104 songs.remove(song) 105 continue 106 filter_match = filter_re.match(title) 107 if filter_match: 108 short_title = filter_match.group(1) 109 if short_title in [ s["title"] for s in songs ]: 110 songs.remove(song) 111 continue 95 112 96 113 def make_pdf(output, title, cover_size): … … 128 145 table_contents = [] 129 146 for song in songs: 130 image = None131 label = Paragraph("%s - %s" % (song["artist"], song["title"]), text_style)132 if "cover" in song and os.path.exists(song["cover"]):133 i mage = song["cover"]134 elif "background" in song and os.path.exists(song["background"]):135 image = song["background"]136 if image:137 image = Image(image, cover_size, cover_size)138 #image.drawHeight = cover_size *I.drawHeight / I.drawWidth139 #image.drawWidth = cover_size140 else:141 image = Spacer(1, cover_size)142 row = [image, label]143 table_contents.append(row)147 try: 148 image = None 149 label = Paragraph("%s - %s" % (song["artist"], song["title"]), text_style) 150 if "image" in song: 151 image = Image(song["image"], cover_size, cover_size) 152 #image.drawHeight = cover_size *I.drawHeight / I.drawWidth 153 #image.drawWidth = cover_size 154 else: 155 image = Spacer(1, cover_size) 156 row = [image, label] 157 table_contents.append(row) 158 except Exception: 159 print >>sys.stderr, "Error when printing %s" % repr(song) 160 raise 144 161 Story.append(Table(table_contents, 145 162 colWidths=(cover_size + 2, col_width - (cover_size + 2)), … … 158 175 parser.add_option("-c", "--cover-size", dest="cover_size", default=32, 159 176 type="int", help="Cover size (default: %default pt)") 177 parser.add_option("--dpi", dest="dpi", default=96, 178 help="Image resolution in Dots Per Inch (default: %default)") 160 179 opts, args = parser.parse_args() 161 180 if len(args) != 0: … … 165 184 if __name__ == '__main__': 166 185 opts = parse_opts() 167 get_songs( )186 get_songs(opts.cover_size, opts.dpi) 168 187 #pprint(songs) 169 188 filter_songs(opts.karaoke)
Note: See TracChangeset
for help on using the changeset viewer.
