About Monkey 2 › Forums › Monkey 2 Code Library › Gravatar profile avatar downloader
This topic contains 2 replies, has 3 voices, and was last updated by
Mark Sibly
1 year, 3 months ago.
-
AuthorPosts
-
December 15, 2017 at 1:04 pm #12307
Download your Gravatar profile picture and display it in your Monkey 2 app.
The very same avatars used on this forum.
It supports custom sizes, custom “no avatar set” images, along with all of Gravatars default avatars like ‘identicon’ and ‘robohash’ etc.I’ve got no function for MD5 hashing, so you’ll have to convert your Gravatar E-Mail address yourself.
Here’s a page that does it: http://www.miraclesalad.com/webtools/md5.php
Make sure you enter your E-Mail in LOWER CASE!If anyone has a MD5 function, I’ll update this.

Download source & wget.exe (required for Windows):
gravatar.zipSource only:
Monkey123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182'Gravatar profile avatar downloader'https://gravatar.com/'By @Hezkore'Explanation'The 'Gravatar' class acts as normal images'Create a new 'Gravatar' and enter your Gravatar E-Mail in lower case but in MD5 hash'Here's an online converter: http://www.miraclesalad.com/webtools/md5.php'For example "my@mail.com" would be aa9c8aff95b1d8eb91ba15adc3e42802'Example: myAvatar=New Gravatar( "aa9c8aff95b1d8eb91ba15adc3e42802" )'IMPORTANT!'Remember to import wget for windows!#If __TARGET__="windows"#Import "bin/wget.exe"#Endif#Import "<std>"#Import "<mojo>"#Import "<mojox>"Using std..Using mojo..Using mojox..Class MyWindow Extends Window'Custom image to use if no avatar is setField customNoAvatarURL:String="http://ichef-1.bbci.co.uk/news/660/cpsprodpb/025B/production/_85730600_monkey2.jpg"Field avatar:GravatarField customNoAvatar:GravatarField noAvatar1:GravatarField noAvatar2:GravatarMethod New()Super.New( "Gravatar", 640, 640 )'New avatar'Use Gravatar default image if no avatar is setavatar=New Gravatar( "c105749e9ce7a0535df2a22265f2fc58", 256 )'New avatar'Use our own custom image URL if no avatar is setcustomNoAvatar=New Gravatar( "00000000000000000000000000000000", 256, customNoAvatarURL )'New avatar'Use 'Robohash' if no avatar is set'Robohash: a generated robot with different colors, faces, etcnoAvatar1=New Gravatar( "00020000000000000200000000000000", 256, GravatarDefault.Robohash )'New avatar'Use 'Identicon' if no avatar is set'Identicon: a geometric pattern based on an email hashnoAvatar2=New Gravatar( "01000100001000010000100000000000", 256, GravatarDefault.Identicon )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawImage( avatar, 32 ,32 )canvas.DrawImage( customNoAvatar, 32+268 ,32 )canvas.DrawImage( noAvatar1, 32 ,32+268 )canvas.DrawImage( noAvatar2, 32+268 ,32+268 )EndEndFunction Main()New AppInstanceNew MyWindowApp.Run()End'GravatarEnum GravatarDefaultMystery=1Identicon=2Monsterid=3Wavatar=4Retro=5Robohash=6Blank=7EndClass Gravatar Extends ImageConst GRAVATAR_DOMAIN:="http://gravatar.com"Const GRAVATAR_DEFSIZE:=40'New Gravatar with Gravatars built in options for empty avatarsMethod New( user:String, size:Int=0, noAvatar:GravatarDefault )Super.New( StartImage( size ) )GetGravatar( user, size, TranslateDefaultEnum( noAvatar ) )End'New Gravatar with custom image for empty avatarsMethod New( user:String, size:Int=0, noAvatar:String="" )Super.New( StartImage( size ) )GetGravatar( user, size, noAvatar )End'Download GravatarMethod GetGravatar( user:String, size:Int, noAvatar:String="" )If size<=0 Then size=GRAVATAR_DEFSIZENew Fiber(Lambda()'Prepare addressLocal src:=GRAVATAR_DOMAIN+"/avatar/"+user+"/default.jpg?s="+sizeIf noAvatar Then src+="&d="+noAvatar'Temp locationLocal tmp:=user+"_"+Millisecs()+"_gravatar.jpg"'Make sure it doesn't exist alreadyDeleteFile( tmp )'Prepare consoleLocal _console:Console=New Console#if __HOSTOS__="macos"Local cmd:="curl -s -o ~q"+tmp+"~q ~q"+src+"~q"#elseLocal cmd:="wget -q -O ~q"+tmp+"~q ~q"+src+"~q"#endif'Download!If Not _console.Run( cmd ) Return'Error?If _console.Process.ExitCodePrint "Gravatar "+_console.Process.ExitCodeReturnEndifIf GetFileSize( tmp )<=1 ThenPrint "Gravatar error downloading"ReturnEndif'Set new imageLocal texture:=New Texture( Pixmap.Load( tmp ), TextureFlags.FilterMipmap )Self.Texture=texture'CleanupDeleteFile( tmp )End)EndPrivate'Just an empty imageMethod StartImage:Pixmap( size:Int )If size<=0 Then size=GRAVATAR_DEFSIZELocal tmpPix:PixmaptmpPix=New Pixmap( size, size )tmpPix.ClearARGB( 0 )Return tmpPixEnd'Translate Enums to proper built in Gravatar avatarsMethod TranslateDefaultEnum:String( v:Int )Select vCase 1 Return "mm" 'mystery manCase 2 Return "identicon"Case 3 Return "monsterid"Case 4 Return "wavatar"Case 5 Return "retro"Case 6 Return "robohash"Case 7 Return "blank"EndReturn ""EndEndDecember 15, 2017 at 5:35 pm #12309That’s awesome….
December 22, 2017 at 9:04 am #12406Cool!
-
AuthorPosts
You must be logged in to reply to this topic.