Package magma :: Package magma :: Module magma_creator_cli
[hide private]
[frames] | no frames]

Source Code for Module magma.magma.magma_creator_cli

  1  #!/usr/bin/env python 
  2  # encoding: utf-8 
  3   
  4  # MAGnet MAnifest Management - Readout and create lists of magnets in yaml format.  
  5  #  
  6  # Copyright © 2008 Arne Babenhauserheide 
  7  #  
  8  # This program is free software: you can redistribute it and/or modify 
  9  # it under the terms of the GNU General Public License as published by 
 10  # the Free Software Foundation, either version 3 of the License, or 
 11  # (at your option) any later version. 
 12  #  
 13  # This program is distributed in the hope that it will be useful, 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 16  # GNU General Public License for more details. 
 17  # 
 18  # You should have received a copy of the GNU General Public License 
 19  # along with this program.  If not, see <http://www.gnu.org/licenses/> 
 20   
 21  """Create magma files interactively via commandline dialogs. 
 22   
 23  Usage: magma_creator_cli.py file1 file2, ... 
 24   
 25  Examples:  
 26      - magma_creator_cli.py input_file.txt input_file2.txt Infinite-Hands--free-software.ogg  
 27   
 28   
 29  This will implement features from magma-for-streaming-mediav0.07.txt 
 30   
 31  It is mainly a proof of concept, yet, and it may change!  
 32   
 33  Ideas:  
 34      - Check, if tagpy or similar are avaible, and if yes, add metadata from the file tags.  
 35  """ 
 36   
 37  # Import basic Magma classes.  
 38  from magma_list import Magma 
 39  from magma_list import MagmaFile  
 40   
 41   
42 -def edit_file(magma_file):
43 """Edit the data inside a magma file - add metadata.""" 44 # First check, which metadata we can get automatically. 45 print "First this script will try to get some values automatically. " 46 automatically_generated_tags = [] 47 48 if not magma_file.name.endswith(".mp3") or magma_file.name.endswith(".ogg"): 49 print "No mp3 or ogg file. We only support mp3 and ogg vorbis at the moment." 50 return 51 52 # Create the audio subdict 53 magma_file.data["audio"] = {} 54 try: 55 from tagpy import FileRef 56 # The basic object from tagpy. 57 filetags = FileRef(magma_file.path) 58 print "yes, FileRef" 59 # The object with audiodata. 60 try: 61 audioprops = filetags.audioProperties() 62 except: pass 63 try: 64 tagfile = filetags.file() 65 except: pass 66 try: 67 tag = filetags.tag() 68 except: pass 69 70 try: 71 # The length of time this file plays. 72 magma_file.data["audio"]["length-ms"] = audioprops.length*1000 73 automatically_generated_tags.append(("length-ms", magma_file.data["audio"]["length-ms"])) 74 except: pass 75 try: 76 # The bitrate of the file. 77 magma_file.data["audio"]["bitrate-kbps"] = audioprops.bitrate 78 automatically_generated_tags.append(("bitrate-kbps", magma_file.data["audio"]["bitrate-kbps"])) 79 except: pass 80 try: 81 # The sampling rate. 82 magma_file.data["audio"]["samplerate-Hz"] = audioprops.sampleRate 83 automatically_generated_tags.append(("samplerate-Hz", magma_file.data["audio"]["samplerate-Hz"])) 84 except: pass 85 try: 86 # And the raw filesize. 87 magma_file.data["size-Bytes"] = tagfile.length() 88 automatically_generated_tags.append(("size-Bytes", magma_file.data["size-bits"])) 89 except: pass 90 91 # Now to the general metatags of the file content. 92 try: 93 # The title of the file 94 magma_file.data["audio"]["title"] = tag.title 95 automatically_generated_tags.append(("title", magma_file.data["audio"]["title"])) 96 except: pass 97 98 try: 99 # The publish year of the file 100 magma_file.data["audio"]["year"] = tag.year 101 automatically_generated_tags.append(("year", magma_file.data["audio"]["year"])) 102 except: pass 103 104 try: 105 # The track number of the file 106 magma_file.data["audio"]["track"] = tag.track 107 automatically_generated_tags.append(("track", magma_file.data["audio"]["track"])) 108 except: pass 109 110 try: 111 # The genre of the file 112 magma_file.data["audio"]["genre"] = tag.genre 113 automatically_generated_tags.append(("genre", magma_file.data["audio"]["genre"])) 114 except: pass 115 116 try: 117 # The comment of the file 118 magma_file.data["audio"]["comment"] = tag.comment 119 automatically_generated_tags.append(("comment", magma_file.data["audio"]["comment"])) 120 except: pass 121 except: pass 122 123 print "The following tags were added:" 124 for i in automatically_generated_tags: 125 print i
126 127 128 129 130
131 -def create_magma_list_interactively(files):
132 """Create magma files interactively via commandline dialogs. 133 134 @param files: A list of files to hash. 135 @type files: List 136 """ 137 magma = Magma(input_files=files) 138 print "Basic magma list: " 139 print magma 140 141 print "------ ------ ------ ------ ------ ------ " 142 143 print "Now we try add metadata using tagpy. " 144 for i in range(len(magma.files)): 145 magma.files[i].path = files[i] 146 edit_file(magma.files[i]) 147 148 print "------ ------ ------ ------ ------ ------ " 149 print "The magma list now looks like this: " 150 print "---" 151 print magma 152 153 ip_port = raw_input("... \nIf you use Gnutella, you can now give a list of IP:PORT pairs seperated by spaces as alternate-locations. For example you can give your clients IP and its port, alternately your dyndns host and your client port. They can greatly increase the performance of downloads but break your anonymity. \nExample: 127.0.0.1:6465 0.0.0.0:9999 example.dyndns.org:1234 \nIf you want to add alt-locs, please add the IP:PORT pairs now: ") 154 alt_locs = ip_port.split() 155 156 try: 157 #from IPy import IP 158 #for i in alt_locs: 159 # ip = IP(i.split(":")[0]) 160 161 if len(alt_locs) > 0: 162 for i in magma.files: 163 i.data["gnutella"]["alt-locs"] = alt_locs 164 except: 165 print "You need the IPy module to add alt-locs, and the IP addresses need to be valid. No alt-locs added." 166 print "To fix it just run 'easy_install IPy', and check the format of the IPs" 167 168 target_file = raw_input("Where shall we save it? ") 169 magma.save(target_file)
170 171 172
173 -def usage():
174 """Usage instructions. 175 176 @return: Usage instructions (String).""" 177 # Just use the docstring. 178 usage = __doc__.split("\n\n")[1:] 179 usage_string = "\n\n".join(usage) 180 return usage_string 181 182 #### Self-Test #### 183
184 -def _test():
185 """Do all doctests. 186 187 @return: None""" 188 from doctest import testmod 189 testmod() 190 191 # If this gets called automatically, load the magma list and print the magnets, or create a magma list. 192 if __name__ == "__main__": 193 _test() 194 from sys import argv 195 if argv[1] in ["--help", "-h", "?", "--usage"]: 196 print usage() 197 else: 198 create_magma_list_interactively(argv[1:]) 199