Guest User

Untitled

a guest
Jul 17th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // How does it work? Hit it with /?username=bradgessler&size=reasonably_small
  2. // and you get your URL (and redirect). Need more documentation? RTFSC.
  3.  
  4. var http = require('http');
  5.  
  6. http.createServer(function(request, response){
  7. var query = require('url').parse(request.url, true).query;
  8.  
  9. if(!query || !query.username){
  10. response.writeHead(412, {'Content-Type': 'text/plain'});
  11. response.end('You need to specify /?username=twitter_name\n');
  12. }
  13. else {
  14. var proxy = http.createClient(80, 'api.twitter.com');
  15. var proxy_request = proxy.request('GET', '/1/users/show.json?screen_name=' + query.username, {'host': 'api.twitter.com'});
  16. var size = (query.size ? query.size : 'normal');
  17.  
  18. proxy_request.end();
  19. proxy_request.on('response', function (proxy_response) {
  20. var body = '';
  21.  
  22. proxy_response.setEncoding('utf8');
  23. proxy_response.on('data', function (chunk) {
  24. body = body.concat(chunk);
  25. });
  26. proxy_response.on('end', function(foo){
  27. var profile = JSON.parse(body);
  28. var imagePath = profile.profile_image_url;
  29. var format = /^(.+)(normal)\.\w{3}$/;
  30. var url = profile.profile_image_url.replace(/(normal)\.(\w+)$/, size + '.$2');
  31.  
  32. response.writeHead(302, {'Content-Type': 'text/plain', 'Location' : url });
  33. response.end(url + '\n');
  34. });
  35. });
  36. }
  37.  
  38. }).listen(7709);
Add Comment
Please, Sign In to add comment