anton_nazarov: (Default)
[personal profile] anton_nazarov
Чего бы не делать, лишь бы не работать!

Читал Хабр, наткнулся на пост про ВК и решил поиграться.

В результате написал две функции для Emacs - заслать текущий буфер на стену и получить плейлист из вконтактика в EMMS (Подробнее тут).

Код ниже, прежде, чем пользоваться, надо сходить по адресу и скопировать из адресной строки значение параметра access_token. Потом это значение надо вписать в переменную vk-auth-token, а в переменную vk-user-id - свой id. Можно использовать M-x customize-variable vk-auth-token и M-x customize-variable vk-user-id.

(defcustom vk-auth-token
"YOUR-TOKEN"
"VK API auth token" :type 'string)

(defcustom vk-user-id YOUR-ID "VK User Id" :type 'number)

(defun vk-post ()
  (interactive)
  (url-retrieve-synchronously
        (format
         "https://api.vk.com/method/wall.post?owner_id=%d&message=%s&v=5.0&access_token=%s"
                        vk-user-id (url-hexify-string (buffer-string))
                        vk-auth-token)))


(defun vk-get-audio (&optional arg)
  (interactive "PUser id:")
  (let ((id (if arg arg vk-user-id)))
    (set-buffer 
        (url-retrieve-synchronously (format
        "https://api.vk.com/method/audio.get?owner_id=%d&need_user=0&v=5.10&access_token=%s"
            id vk-auth-token) 't 't))
    
    (set-buffer-multibyte 't)
    (search-forward "{")
    (backward-char)
    (let* (
           (json-array-type 'list)
           (items (cdadar (json-read))) (vk-emms-buffer (emms-playlist-new)))
      (set-buffer vk-emms-buffer)
      (dolist (item items)
        (let ((track 
               (emms-track 'url (cdr (assoc 'url item)))))
          (emms-track-set track 'info-title  (cdr (assoc 'title item)))
          (emms-track-set track 'info-artist (cdr (assoc 'artist item)))
          (emms-playlist-insert-track track)))
      (switch-to-buffer vk-emms-buffer))))
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

anton_nazarov: (Default)
anton_nazarov

September 2014

S M T W T F S
 123456
78910111213
14 151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 25th, 2017 06:33 pm
Powered by Dreamwidth Studios