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

Source Code for Module magma.magma.magma_creator_cli_proof_of_concept

  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 ask_audio(files, magma):
43 """Ask, whether one of the files is an audio file.""" 44 print "Now, let's make it a audio list." 45 print "" 46 audio = raw_input("Is one of the file a audio file (yes/No)? ") 47 # Ask until the user 48 while not audio.lower() in ["yes", "no", "", "y", "n"]: 49 audio = raw_input("Is one of the file a audio file? Please answer either 'yes' or 'no'. Just clicking enter skips: ") 50 # If we have no audio files, we return instantly. 51 if audio.lower() == "no" or audio.lower() == "n" or audio.lower() == "": 52 return 53 54 # Else, the answer was yes, and we can go on. 55 # First get the file which is a audio file. TODO: Add support for multiple mutimedia files. 56 57 audio = raw_input("Please enter which file it is (1 or 2 or ...): ") 58 while not audio in [str(i) for i in range(1, len(magma.files) + 1)]: 59 audio = raw_input("Please enter a valid number (1 or 2 or ...): ") 60 print "Now onwards to editing file", audio + ":", magma.files[int(audio) -1].name 61 file_to_edit = magma.files[int(audio) -1] 62 file_to_edit.path = files[int(audio) -1] 63 edit_file(file_to_edit) 64 65 print "So now, the file looks like this:" 66 67 print magma 68 69 magma_filename = raw_input("Please enter a name to which to save the file. It should end on .magma: ") 70 magma.save(magma_filename) 71 72 print "Magma file saved as", magma_filename 73 74 print "It contains the following magnets:" 75 for i in magma.magnets: 76 print i 77 78
79 -def edit_file(magma_file):
80 """Edit the data inside a magma file - add metadata.""" 81 # First check, which metadata we can get automatically. 82 print "First this script will try to get some values automatically. " 83 automatically_generated_tags = [] 84 # Create the audio subdict 85 magma_file.data["audio"] = {} 86 try: 87 from tagpy import FileRef 88 # The basic object from tagpy. 89 filetags = FileRef(magma_file.path) 90 # The object with audiodata. 91 audioprops = filetags.audioProperties() 92 tagfile = filetags.file() 93 try: 94 # The length of time this file plays. 95 magma_file.data["audio"]["length-ms"] = audioprops.length*1000 96 automatically_generated_tags.append(("length-ms", magma_file.data["audio"]["length-ms"])) 97 except: pass 98 try: 99 # The bitrate of the file. 100 magma_file.data["audio"]["bitrate-kbps"] = audioprops.bitrate 101 automatically_generated_tags.append(("bitrate-kbps", magma_file.data["audio"]["bitrate-kbps"])) 102 except: pass 103 try: 104 # The sampling rate. 105 magma_file.data["audio"]["samplerate-Hz"] = audioprops.sampleRate 106 automatically_generated_tags.append(("samplerate-Hz", magma_file.data["audio"]["samplerate-Hz"])) 107 except: pass 108 try: 109 # And the raw filesize. 110 magma_file.data["size-Bytes"] = tagfile.length() 111 automatically_generated_tags.append(("size-Bytes", magma_file.data["size-bits"])) 112 except: pass 113 except: 114 pass 115 116 print "The following tags were added:" 117 for i in automatically_generated_tags: 118 print i 119 # TODO: Ask the user which of the tags to add. 120 121 magma_file.data["title"] = raw_input("Please enter the title of the file: ") 122 magma_file.data["description"] = raw_input("Please enter a description of the file: ") 123 124 # Now add one of the artists. 125 magma_file.data["audio"]["artists"] = [] 126 artist1 = {} 127 artist1["name"] = raw_input("Please enter the name of its artist: ") 128 artist1["website"] = raw_input("Please enter an url to the website of the artist: ") 129 magma_file.data["audio"]["artists"] .append(artist1) 130 131 # And add some more data. 132 magma_file.data["audio"]["album"] = raw_input("Please enter the name of the album: ") 133 magma_file.data["year"] = raw_input("Please enter the year when this work was published: ") 134 magma_file.data["audio"]["tracknumber"] = raw_input("Please enter the tracknumber of this file: ")
135 136 137 138
139 -def create_magma_list_interactively(files):
140 """Create magma files interactively via commandline dialogs. 141 142 @param files: A list of files to hash. 143 @type files: List 144 """ 145 magma = Magma(input_files=files) 146 print "Basic magma list: " 147 print magma 148 149 ask_audio(files, magma)
150 151 152 153
154 -def usage():
155 """Usage instructions. 156 157 @return: Usage instructions (String).""" 158 # Just use the docstring. 159 usage = __doc__.split("\n\n")[1:] 160 usage_string = "\n\n".join(usage) 161 return usage_string 162 163 #### Self-Test #### 164
165 -def _test():
166 """Do all doctests. 167 168 @return: None""" 169 from doctest import testmod 170 testmod() 171 172 # If this gets called automatically, load the magma list and print the magnets, or create a magma list. 173 if __name__ == "__main__": 174 _test() 175 from sys import argv 176 create_magma_list_interactively(argv[1:]) 177