Skip to content

Instantly share code, notes, and snippets.

@yamamotoj
Forked from matchy256/rec_radiko.sh
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamamotoj/a349749be6f52a90af40 to your computer and use it in GitHub Desktop.
Save yamamotoj/a349749be6f52a90af40 to your computer and use it in GitHub Desktop.
from evernote.api.client import EvernoteClient
import evernote.edam.type.ttypes as Types
import sys
import hashlib
import time
import mimetypes
# dev_token = "S=s1:U=90bdf:E=1544ee17084:C=14cf7304108:P=1cd:A=en-devtoken:V=2:H=e20c8d80dd09580a5fb802a77d1c0eb8"
dev_token="S=s1:U=90bdf:E=154519873c0:C=14cf9e746d0:P=81:A=jyamamoto:V=2:H=bf7a268d7243962ccf4d49e520b21049"
client = EvernoteClient(token=dev_token)
userStore = client.get_user_store()
user = userStore.getUser()
arg = sys.argv[1]
tagName = sys.argv[2]
filedata = open( arg, 'rb').read()
md5 = hashlib.md5()
md5.update(filedata)
hashHex = md5.hexdigest()
data = Types.Data()
data.size = len(filedata)
data.bodyHash = hashHex
data.body = filedata
resource = Types.Resource()
resource.mime = mimetypes.guess_type( arg)[0]
resource.data = data
noteStore = client.get_note_store()
def get_radiko_book (noteStore) :
notebooks = filter(lambda n : n.name == 'radiko', noteStore.listNotebooks())
if len(notebooks) > 0 :
return notebooks[0]
notebook = Types.Notebook()
notebook.name = "radiko"
notebook = noteStore.createNotebook(notebook)
return notebook
def get_tag (noteStore, tagName) :
tags = filter(lambda n : n.name == tagName, noteStore.listTags())
if len(tags) > 0 :
return tags[0]
tag = Types.Tag()
tag.name = tagName
return noteStore.createTag(tag)
notebook = get_radiko_book(noteStore)
note = Types.Note()
note.notebookGuid = notebook.guid
note.title = arg.split("/")[-1]
note.content = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">'
note.content += '<en-note>'
link = '<en-media type="%s" hash="%s"/>' % (resource.mime, hashHex)
note.content += link
note.content += '</en-note>'
note.resources = [ resource ]
tag = get_tag(noteStore, tagName)
note.tagGuids = [tag.guid]
note = noteStore.createNote(note)
# Python OAuth example
import evernote.edam.userstore.constants as UserStoreConstants
import evernote.edam.type.ttypes as Types
from evernote.api.client import EvernoteClient
##
# Helper function to turn query string parameters into a
# Python dictionary
##
def parse_query_string(authorize_url):
uargs = authorize_url.split('?')
vals = {}
if len(uargs) == 1:
raise Exception('Invalid Authorization URL')
for pair in uargs[1].split('&'):
key, value = pair.split('=', 1)
vals[key] = value
return vals
##
# Create an instance of EvernoteClient using your API
# key (consumer key and consumer secret)
##
client = EvernoteClient(
consumer_key = 'jyamamoto',
consumer_secret = '33ec622b2b440830',
sandbox = True
)
##
# Provide the URL where the Evernote Cloud API should
# redirect the user after the request token has been
# generated. In this example, localhost is used; note
# that in this example, we're copying the URLs manually
# and that, in production, this URL will need to
# automatically parse the response and send the user
# to the next step in the flow.
##
request_token = client.get_request_token('http://example.com')
##
# Prompt the user to open the request URL in their browser
##
print "Paste this URL in your browser and login"
print
print '\t'+client.get_authorize_url(request_token)
print
print '-------'
##
# Have the user paste the resulting URL so we can pull it
# apart
##
print "Paste the URL after login here:"
authurl = raw_input()
##
# Parse the URL to get the OAuth verifier
##
vals = parse_query_string(authurl)
##
# Use the OAuth verifier and the values from request_token
# to built the request for our authentication token, then
# ask for it.
##
auth_token = client.get_access_token(
request_token['oauth_token'],
request_token['oauth_token_secret'],
vals['oauth_verifier']
)
print auth_token
##
# Create a new EvernoteClient instance with our auth
# token.
##
client = EvernoteClient(token=auth_token)
##
# Test the auth token...
##
userStore = client.get_user_store()
user = userStore.getUser()
##
# If our username prints, it worked.
##
print user.username
#!/bin/bash
LANG=ja_JP.utf8
pid=$$
date=`date '+%Y-%m-%d-%H_%M'`
playerurl=http://radiko.jp/player/swf/player_3.0.0.01.swf
playerfile="/tmp/player.swf"
keyfile="/tmp/authkey.png"
outdir="."
if [ $# -le 1 ]; then
echo "usage : $0 channel_name duration(minuites) [outputdir] [prefix]"
exit 1
fi
if [ $# -ge 2 ]; then
channel=$1
DURATION=`expr $2 \* 60`
fi
if [ $# -ge 3 ]; then
outdir=$3
fi
PREFIX=${channel}
if [ $# -ge 4 ]; then
PREFIX=$4
fi
#
# get player
#
if [ ! -f $playerfile ]; then
wget -q -O $playerfile $playerurl
if [ $? -ne 0 ]; then
echo "failed get player"
exit 1
fi
fi
#
# get keydata (need swftool)
#
if [ ! -f $keyfile ]; then
swfextract -b 14 $playerfile -o $keyfile
if [ ! -f $keyfile ]; then
echo "failed get keydata"
exit 1
fi
fi
if [ -f auth1_fms_${pid} ]; then
rm -f auth1_fms_${pid}
fi
#
# access auth1_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--post-data='\r\n' \
--no-check-certificate \
--save-headers \
-O auth1_fms_${pid} \
https://radiko.jp/v2/api/auth1_fms
if [ $? -ne 0 ]; then
echo "failed auth1 process"
exit 1
fi
#
# get partial key
#
authtoken=`perl -ne 'print $1 if(/x-radiko-authtoken: ([\w-]+)/i)' auth1_fms_${pid}`
offset=`perl -ne 'print $1 if(/x-radiko-keyoffset: (\d+)/i)' auth1_fms_${pid}`
length=`perl -ne 'print $1 if(/x-radiko-keylength: (\d+)/i)' auth1_fms_${pid}`
partialkey=`dd if=$keyfile bs=1 skip=${offset} count=${length} 2> /dev/null | base64`
#echo "authtoken: ${authtoken} \noffset: ${offset} length: ${length} \npartialkey: $partialkey"
rm -f auth1_fms_${pid}
if [ -f auth2_fms_${pid} ]; then
rm -f auth2_fms_${pid}
fi
#
# access auth2_fms
#
wget -q \
--header="pragma: no-cache" \
--header="X-Radiko-App: pc_1" \
--header="X-Radiko-App-Version: 2.0.1" \
--header="X-Radiko-User: test-stream" \
--header="X-Radiko-Device: pc" \
--header="X-Radiko-Authtoken: ${authtoken}" \
--header="X-Radiko-Partialkey: ${partialkey}" \
--post-data='\r\n' \
--no-check-certificate \
-O auth2_fms_${pid} \
https://radiko.jp/v2/api/auth2_fms
if [ $? -ne 0 -o ! -f auth2_fms_${pid} ]; then
echo "failed auth2 process"
exit 1
fi
#echo "authentication success"
areaid=`perl -ne 'print $1 if(/^([^,]+),/i)' auth2_fms_${pid}`
#echo "areaid: $areaid"
rm -f auth2_fms_${pid}
#
# get stream-url
#
if [ -f ${channel}.xml ]; then
rm -f ${channel}.xml
fi
wget -q "http://radiko.jp/v2/station/stream/${channel}.xml"
stream_url=`echo "cat /url/item[1]/text()" | xmllint --shell ${channel}.xml | tail -2 | head -1`
url_parts=(`echo ${stream_url} | perl -pe 's!^(.*)://(.*?)/(.*)/(.*?)$/!$1://$2 $3 $4!'`)
rm -f ${channel}.xml
file_path="${outdir}/${PREFIX}_${date}.mp3"
#
# rtmpdump
#
#rtmpdump -q \
rtmpdump \
-r ${url_parts[0]} \
--app ${url_parts[1]} \
--playpath ${url_parts[2]} \
-W $playerurl \
-C S:"" -C S:"" -C S:"" -C S:$authtoken \
--live \
--stop ${DURATION} \
--flv "/tmp/${channel}_${date}"
ffmpeg -loglevel quiet -y -i "/tmp/${channel}_${date}" -acodec libmp3lame -ab 128k "${outdir}/${PREFIX}_${date}.mp3"
if [ $? = 0 ]; then
rm -f "/tmp/${channel}_${date}"
python /home/pi/rec_radiko/evernote_upload.py ${file_path} ${PREFIX}
# rm -f ${file_path}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment