Looks like the Great Firewall or something like it is preventing you from completely loading www.skritter.com because it is hosted on Google App Engine, which is periodically blocked. Try instead our mirror:

legacy.skritter.cn

This might also be caused by an internet filter, such as SafeEyes. If you have such a filter installed, try adding appspot.com to the list of allowed domains.

api sample code?

wb   October 1st, 2013 3:23a.m.

Hi,

the new api looks really interesting, will there be some sample code in the docs? Python/javascript to get started?

mratranslate   October 1st, 2013 4:36a.m.

Sometimes I wonder if Skritter is a language learning tool or if this a computer programming club...

俞翰森   October 1st, 2013 5:06a.m.

I think it is a programming club:-) Code is equally beautiful as characters are so they have the same attraction to some of us nerds.

lechuan   October 1st, 2013 9:40a.m.

@xiaomai5, Either way you're learning a language: Mandarin, Japanese, Python, Javascript.....

马洲屹   October 1st, 2013 6:36p.m.

@lechuan...touché!

scott   October 1st, 2013 8:02p.m.

There's some sample code in some of the endpoints showing some of the ways I imagine the API would be used. For example, at the bottom of here:

http://www.skritter.com/api/v0/docs/endpoints/vocabs

There's some sample Python code. Granted, I didn't test much of that, it's more pseudo-code that happens to be written Python style. But it's a start!

Would you want more sample code like that? Or some other format?

wb   October 2nd, 2013 5:05a.m.

Oh right I should have looked a bit longer. ;-)

It might be a good idea to add one complete working script where anyone can just fill in username and password and then get some vocab data...
I guess it's pretty easy but I never did any networking stuff so maybe it's just me. ;-)

mcfarljw   October 3rd, 2013 10:23p.m.

@scott, down the road we could add in a toggle box for the examples to switch between the current Python code and JavaScript using jQuery.

dusan   October 7th, 2013 9:20p.m.

Here is a small, basic Python code sample to get vocabs from the API:

https://gist.github.com/djuretic/6877868

It's not complete (e.g. no pagination), but I hope it helps!

anterya   October 9th, 2013 1:56p.m.

@dusan, thank you for providing a complete working sample script, it was really helpful!

I'm completely new to Python (but not to programming, fortunately), so I had to find the hard way that your sample (as well, I guess, as the code samples in the API documentation) only works under Python 2.x, but not under Python 3.x. Is this correct, or was I doing something wrong?

--------------------------------------------
Under Python-3.3.2 I'm getting an error response, as follows:

File "C:\Python3386\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u6d25' in position 4: character maps to

Under Python-2.7.5 everything works fine.

anterya   October 9th, 2013 2:09p.m.

I also have a question to Scott. In order to make the code sample from dusan work, I had to register a new "client" account with Skritter useing the coupon code APICLIENT, as described in the docs for "Authentication" (http://www.skritter.com/api/v0/docs/authentication). It worked fine, but I noticed that my new "API" account is only valid for two weeks, as a "normal" new account, and the Skritter web site recommended that I should provide billing information. Is this some kind of misunderstanding, or do I really have to pay for the second account if I wish to continue to access the data from my "main" user account via the Skritter API? Thanks.

(Just a clarification: I don't need a second account except for the purpose of accessing data from my main account via the Skritter API.)

dusan   October 9th, 2013 2:14p.m.

@anterya, by chance do you have a more complete stacktrace? I think that the problem is Windows-related.

anterya   October 9th, 2013 2:31p.m.

@dusan, here you are:

[{'audio': '/sounds?file=jin1.mp3',
'changed': 1346699890,
'definitions': {'en': 'saliva; sweat; ford (river crossing); Tianjin (abbr.)'},
'id': Traceback (most recent call last):
File "vocabs-antr.py", line 58, in
pprint(vocabs)
File "C:\Python3386\lib\pprint.py", line 55, in pprint
printer.pprint(object)
File "C:\Python3386\lib\pprint.py", line 136, in pprint
self._format(object, self._stream, 0, 0, {}, 0)
File "C:\Python3386\lib\pprint.py", line 232, in _format
context, level)
File "C:\Python3386\lib\pprint.py", line 193, in _format
allowance + 1, context, level)
File "C:\Python3386\lib\pprint.py", line 245, in _format
write(rep)
File "C:\Python3386\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u6d25' in position 4: character maps to

-----------------
Note: at the very end of the stack trace, after "maps to" there's also the word "undefined" in the "less-than, greater-than" brackets, which for some reason is not shown on the forum thread page, perhaps because it is interpreted as an unknown HTML/XML tag.

An addition: same problem: after "line 58, in" should be the word "module" in the same HTML-tag-style brackets, which is now invisible.

dusan   October 9th, 2013 8:30p.m.

@anterya, it seems that the problem is related to the Windows console. Replace the last line of the script with this, to save the output to a file:

with open('output.txt', 'w', encoding='utf-8') as f:
pprint(vocabs, f)

(Indent the pprint line, here the indentation is lost)

nick   October 13th, 2013 12:46p.m.

You don't need to pay for APICLIENT accounts; just let them expire. You're right, it should probably not bother you about billing and subscription emails and such for them, but it's a side effect of them also being normal Skritter accounts.

俞翰森   October 21st, 2013 9:02a.m.

I am having very fun with this and have been able to get almost everything i where looking for but one: include_sentences. I do get the sentence but not the related parts like pinyin and translation. When reading the API manual I do think I should be be able to get them, is that true or do I miss something?

mcfarljw   October 21st, 2013 9:11a.m.

I just a test call to the API for two items by id with include_vocab and include_sentences set to true. It returned two sentences that included all of the information about them.

Can you provide some exact details about the call you're making to the API? (just so I can better recreate it)

俞翰森   October 21st, 2013 6:39p.m.

I am am using this:

get_vocabs(token, limit=10,Bol='True',myfields='sentenceId,writing,reading,definitions'):
params = {

'limit': limit,
'bearer_token': token,
'include_sentences': Bol,
'sentence_fields': myfields,
'include_decomps': Bol,
'include_top_mnemonics': Bol
}



This gives e.g. this result:

'containedVocabIds': ['zh-一-0', 'zh-个-0'],
'definitions': {'en': 'one; a', 'sv': 'en; ett'},
'id': 'zh-一个-0',
'lang': 'zh',
'reading': 'yi2ge5',
'sentenceId': 'zh-要是有一个很好的中文软件大家方便用那多好啊-0',
'starred': False,
'style': 'simp',
'toughness': 2,
'toughnessString': 'easier',
'writing': '一个'},
{'audio': '/sounds?file=yi2hui4r5.mp3',


I will try some other settings today grabbing words by ID if that has anything to do with this.

mcfarljw   October 21st, 2013 7:47p.m.

That results looks ok to me and will always contain the sentenceId. The Vocab object will only ever contain the sentenceId and never the other information. You're json response should also contain another object called Sentences. For example,

Object {cursor:'some-long-string', Vocabs: Array[10], Sentences: Array[10], statusCode: 200};

Then you can link the sentenceId in the Vocab to a sentence. What information does it include when you step back a level?

anterya   October 22nd, 2013 5:08p.m.

@dusan: Sorry for replying late. Yes, what you suggested fixed the problem. Thank you! (I guess, it has something to do with the fact that Windows can't show Unicode symbols in a DOS prompt.)

@nick: Thank you for the explanation!

This forum is now read only. Please go to Skritter Discourse Forum instead to start a new conversation!