9af900e5f2f48ac53b3e4f2a056a5ac1b1317640 — Zack Scholl 3 years ago bfc59b8
Added perma.cc support
3 files changed, 59 insertions(+), 14 deletions(-)

M app.py
M lib.py
M templates/index.html
M app.py => app.py +4 -1
@@ 14,7 14,10 @@ request.get_json(force=True)
  	print request.json
  	text = request.json['text']
- 	return jsonify(text=replaceText(text))
+ 	if 'apikey' in request.json and len(request.json['apikey'])>0:
+ 		return jsonify(text=replaceText(text,request.json['apikey']))
+ 	else:
+ 		return jsonify(text=replaceText(text,False))
  
  if __name__ == "__main__":
      app.run(host='0.0.0.0',port=8012)= 
\ No newline at end of file

M lib.py => lib.py +37 -8
@@ 1,9 1,13 @@ # -*- coding: utf-8 -*-
  import sys
  import re
- from multiprocessing import Pool
+ import json
+ from multiprocessing import Pool, cpu_count
+ 
  import requests
  
+ from settings import *
+ 
  
  
  def isurl(s):


@@ 13,7 17,7 @@ return False
  
    
- def getPermalink(url):
+ def getWebArchiveLink(url):
    # will need to handle cases that it can't find, like for http://w3techs.com/technologies/overview/javascript_library/all
    if 'web.archive' in url:
      return url,url


@@ 30,25 34,49 @@ except:
        print url
        return url,url
+     
+ def getPermaccLink(dat):
+   # will need to handle cases that it can't find, like for http://w3techs.com/technologies/overview/javascript_library/all
+   url = dat[0]
+   apikey = dat[1]
+   payload = {'url': url, 'title': url}
+   permacc_url =  'https://api.perma.cc/v1/archives/?api_key=' + apikey
+   r = requests.post(permacc_url, data = json.dumps(payload))
+   print r.status_code
+   if r.status_code == 201:
+     result = json.loads(r.text)
+     print json.dumps(result,indent=4)
+     return url,str('http://perma.cc/' + result['guid'])
+   else:
+     return url,url
    
  
- def replaceText(text_test):
+ def replaceText(text_test,apikey):
    urls = []
+   urls_in_order = []
    for url in  re.findall(r'(https?://[^\s]+)', text_test):
      newurl = url.split('"')[0].split('<')[0]
      while newurl[-1] == '.' or newurl[-1] == ')' or newurl[-1] == '!':
        newurl = newurl[:-1]
-     urls.append(newurl)
+     if not apikey:
+       urls.append(newurl)
+     else:
+       urls.append((newurl,apikey))
+     urls_in_order.append(newurl)
  
  
-   p = Pool(5)
+   f = getWebArchiveLink
+   if apikey:
+     f = getPermaccLink
+   p = Pool(cpu_count())
    conversion = {}
-   for result in p.map(getPermalink, list(set(urls))):
+   for result in p.map(f, list(set(urls))):
      conversion[result[0]] = result[1]    
    p.terminate()
  
+   print conversion
    curPos = 0
-   for url in urls:
+   for url in urls_in_order:
      if url in text_test[curPos:]:
        print url
        print conversion[url]


@@ 57,6 85,7 @@ text_test = text_test[0:curPos] + text_test[curPos:].replace(url,conversion[url],1)
        curPos = newPos
  
-   return text_test
+   return text_test  
+ 
  
  

M templates/index.html => templates/index.html +18 -5
@@ 40,6 40,7 @@ <span class="icon-bar"></span>
                      <span class="icon-bar"></span>
                  </button>
+ 
                  <a class="navbar-brand page-scroll" href="#page-top">Permalinker</a>
              </div>
  


@@ 69,12 70,24 @@ <div class="row">
                  <div class="col-lg-12">
                      <h1>Permalinker</h1>
-                     <p class="lead">Don't let your links rot! Type in any series of links, or text with links, and this tool will automatically make them permanent!</p>
-                     <p>Supports Markdown, HTML, or any sort of text or links.</p>
+                     <p class="lead">Don't let your links rot! Type in any series of links, or text with links, and this tool will automatically make them permanent! Supports Markdown, HTML, or any sort of text or links.</p>
+                     <p>Automatically uses the <a href="https://archive.org/index.php">Internet Archive</a>. If you'd like to use <a href="https://perma.cc/login">perma.cc</a> instead, simply enter your <a href="https://perma.cc/settings/tools" target="_blank">API key</a>.</p>
                      <textarea placeholder="Type in text or links or both." rows="10" style="width:100%;" name="comment[text]" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true" id="inputtext"></textarea>
-                     <a class="btn btn-default" id="submitbutton">Generate permanent links!</a>
+                 </div>
+                 <div class="col-xs-12">
+ <form class="form-inline" action="javascript:void(0);">
+   <div class="form-group">
+     <label class="sr-only" for="apikey">Perm.cc API key</label>
+     <input type="input" class="form-control" id="apikey" placeholder="Perm.cc API key">
+   </div>
+   <button type="submit" class="btn btn-default" id="submitbutton">Generate permalinks!</button>
+ </form>
+ 
+                 </div>
+                 <div class="col-lg-12">
                      <textarea placeholder="Type in text or links or both." rows="10" style="width:100%;" name="comment[text]" class="ui-autocomplete-input" autocomplete="off" role="textbox" aria-autocomplete="list" aria-haspopup="true" id="outputtext"></textarea>
                  </div>
+                 </div>
              </div>
          </div>
      </section>


@@ 157,12 170,12 @@ $(document).ready(function(){
  $notloading.show();
  
-       $("a#submitbutton").click(function(e){
+       $("#submitbutton").click(function(e){
            e.preventDefault();
                $.ajax({
                    type: 'POST',
                    url: '/permanize',
-                   data: JSON.stringify({text:$("textarea#inputtext").val()}, null, '\t'),
+                   data: JSON.stringify({text:$("textarea#inputtext").val(),apikey:$("#apikey").val()}, null, '\t'),
                    contentType: 'application/json;charset=UTF-8',
                    success: function(result){
                        console.log(result)