#29 Nuke shorturl hack, make it prettier
Merged 4 years ago by ankursinha. Opened 4 years ago by codeblock.
codeblock/fpaste shorturl-hack-nuke  into  master

file modified
+3 -10
@@ -96,16 +96,9 @@ 

      except urllib.error.URLError:

          return False

  

-     # We know that short_url is always the third last line in the json output

-     # Iterating over each line is a bad idea. It'll break everytime it

-     # encounters a "short_url" string.

-     try:

-         result = json.loads(

-             '{' + f.readlines()[-3].decode() + '}')['short_url']

-     except ValueError as e:

-         return False

-     return result

- 

+     return (json.loads(f.read().decode('utf-8'))

+                 .get('result', {})

+                 .get('short_url', False))

  

  def paste(text, options):

      """Send text to paste server and return the URL."""

Rather than doing hacks which break the JSON specification (which states that fields of a dictionary can be in any order), just read the whole thing into memory and pull out the key if it exists.

The bottleneck in fpaste speed will always be network latency; we lose nothing by doing this.

Pull-Request has been merged by ankursinha

4 years ago

Thanks! Merged!

I don't know if we'll make another release off master, though, given that we're moving off to modernpaste soon.

Metadata