Skip to content

Commit

Permalink
Audio albums sorting and README update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rast1234 committed Jul 4, 2013
1 parent 0559848 commit e8234e2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
47 changes: 41 additions & 6 deletions PostParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,34 @@ def escape(name):
"""Escape the filename"""
result = unicode(re.sub('[^+=\-()$!#%&,.\w\s]', '_', name, flags=re.UNICODE).strip())
#print("\t{}\n\t{}".format(name, result))
return result[:100]
return result[:250]

def get_album_name(*args):
"""Try to find and cache album name"""
names = {}
def inner_function(owner, album, args):
"""Wrapped get_album_name function"""
key = "{}_{}".format(str(owner), str(album))
try:
return names[key]
except KeyError:
#get all albums once
print("Building album index...")
(data, json_stuff) = call_api("audio.getAlbums", [("owner_id", owner), ("offset", 0), ("count", 1)], args)
count = data[0]
for x in xrange(count):
(data, json_stuff) = call_api("audio.getAlbums", [("owner_id", owner), ("offset", x), ("count", 1)], args)
oid = data[1][u'owner_id']
aid = data[1][u'album_id']
val = data[1]['title']
tmp_key = "{}_{}".format(oid, aid)
names[tmp_key] = val
logging.info(u"Found album: owner={} id={} name={}".format(oid, aid, val))
print("Album index OK, count: {}".format(count))
return names[key]
return inner_function
get_album_name = get_album_name() # boo!


class PostParser(object):
"""Parses given post into data lists (text, music, photos, info, etc.)
Expand Down Expand Up @@ -233,19 +260,26 @@ def dl_photos_list(self, data):
self.dl_photo(x)

def dl_audio(self, data):
initial_data = data
aid = data["aid"]
owner = data["owner_id"]
request = "{}_{}".format(owner, aid)
(audio_data, json_stuff) = call_api("audio.getById", [("audios", request), ], self.args)
album = ''
try:
data = audio_data[0]
name = u"{artist} - {title}.mp3".format(**data)
self.save_url(data["url"], name)
artist = data['artist'][:100]
title= data['title'][:100]
name = u"{} - {}.mp3".format(artist, title)
album = data['album']
album = get_album_name(owner, album, self.args)
album = escape(album)
make_dir(self.post_directory, album)
self.save_url(data["url"], name, album)
except IndexError: # deleted :(
logging.warning("Deleted track: {}".format(str(data)))
logging.warning("Deleted track: {}".format(str(initial_data)))
return


# store lyrics if any
try:
lid = data["lyrics_id"]
Expand All @@ -254,7 +288,8 @@ def dl_audio(self, data):
(lyrics_data, json_stuff) = call_api("audio.getLyrics", [("lyrics_id", lid), ], self.args)
text = lyrics_data["text"].encode('utf-8')
name = escape(name)
f_name = path.join(self.post_directory, name+'.txt')
f_name = path.join(self.post_directory, album)
f_name = path.join(f_name, name+'.txt')
# escape!
out_file = open(f_name, 'a+')
out_file.write(text)
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ VK dumper - save stuff from your vk.com to local drive
* Download audio tracks
* Correct fime naming
* Text stored also (if available)
* **TODO:** sort by playlists
* Sort by playlists (as subfolders)
* Download docs
* Auto-change filename if exists
* Write correct extension
Expand All @@ -24,11 +24,10 @@ VK dumper - save stuff from your vk.com to local drive

**Known limitations, bugs and other considerations:**

* Unable to download videos (Someone need this?)
* Unable to download videos (Anyone need this?)
* Unable to download note comments (Maybe an unneeded feature?)
* Tested on a particular user, not tested on a group
* Tested on a particular user, audio tested on a group
* Can't grant access by itself, so you need to enter auth token manually (see below)
* Not tested on Windows
* If wall, documents or audio list has been modified during work, it **will** cause unpredictable things.
* Sometimes you need to solve CAPTCHA (interactively)

Expand Down Expand Up @@ -76,6 +75,8 @@ Everything will be stored in specified directory, say, `some_dir`:
| +---- Artist1 - Track.mp3
| +---- Artist2 - Track.mp3
| +---- Artist2 - Track.mp3.txt (text for that song)
| +---- Album1 (audio album name)
| | +---- Artist - track.mp3 (tracks in that album)
| +---- ...
+---- post_1234 (wall post id)
| +---- text.html (post text)
Expand Down

0 comments on commit e8234e2

Please sign in to comment.