Skip to content

Commit

Permalink
new features ! Send files and text to your phone !
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaaoBlues committed Jul 4, 2022
1 parent eef899e commit 1a25674
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 40 deletions.
46 changes: 40 additions & 6 deletions copypasta.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
from readline import get_current_history_length, get_history_length
import sys
import socket
from flask import Flask, render_template, abort,jsonify,send_file,request,redirect,flash
from itsdangerous import json
from flask import Flask, render_template, abort,jsonify,send_file,request,redirect,flash, send_from_directory
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from requests import get
from os import path,remove, startfile, rename,chdir
import PIL.Image as Image
from io import BytesIO
from shutil import copyfile
try:
import win32clipboard
except ImportError:
Expand Down Expand Up @@ -456,6 +457,7 @@ def api(api_req):

if request.remote_addr == "127.0.0.1":



if api_req == "get_history":

Expand Down Expand Up @@ -498,10 +500,30 @@ def api(api_req):

return jsonify({"success":"shutting down CopyPasta server..."})

elif api_req == "gen_otdl_url":

# keep main window hidden
root = Tk()
root.attributes("-topmost", True)
root.withdraw()
# open file dialog
file_path = askopenfilename(parent=root)

if not file_path:
return jsonify({"Error":"no file selected"})


if not path.exists("static/ot_upload"):
mkdir("static/ot_upload")

#move file in a downloadable directory
copyfile(file_path,path.join(APP_PATH,f"static/ot_upload/{path.basename(file_path)}"))

# return url to javascript for qr code generation and notification
return f"http://{get_private_ip()}:21987/download?file={path.basename(file_path)}"
else:
return jsonify({"Error" : "wrong api call"})


else:

if api_req == "ping":
Expand All @@ -512,9 +534,21 @@ def api(api_req):
return abort(403)


@app.route("/download",methods=["GET"])



def download():

try:
file = request.args.get("file",type=str)
except:
return jsonify({"Error":"Invalid url parameter"})

file_path = path.join(APP_PATH,"static","ot_upload",file)
if path.exists(file_path):
print("exists",file_path)
return send_file(file_path,as_attachment=True)
else:
return jsonify({"Error":"This file does not exists or have already been downloaded one time."})


@app.route("/upload",methods=["POST"])
Expand Down
37 changes: 36 additions & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@
<ul class="navbar-nav mr-auto">
<li class="nav-item active"> <a class="nav-link active fs-5" href="https://copypastaofficial.github.io"><i class="bi bi-info-square"></i></a></li>
</ul>
</div>
<ul class="navbar-nav mr-auto">
<li class="nav-item active"> <a class="nav-link active fs-5" onclick="send_file_to_phone()">Send a file to my phone </a></li>

</ul>
<ul class="navbar-nav mr-auto">
<li class="nav-item active"> <a class="nav-link active fs-5" onclick="send_text_to_phone()">Send a text/url to my phone </a></li>
</ul>
</div>

</div>
</nav>

Expand Down Expand Up @@ -140,6 +148,33 @@ <h1>Last files/data sent :</h1>
<script>


function send_text_to_phone(){
let text = prompt('Type/paste your text/url here (max 4000 characters)');
if(text.length > 4000){
text = text.substring(0,4000);
}
let qr_img = "<h3>Scan this qr code with your phone :)<h3><br><img src=\"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=" +text+"\" >";
document.getElementById("alert_container").innerHTML = "<div class=\"alert alert-primary alert-dismissible fade show\" role=\"alert\">"+qr_img+
"<button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"></button>"+
"</div>";
}

function send_file_to_phone(){

var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://127.0.0.1:21987/api/gen_otdl_url", false ); // false for synchronous request
xmlHttp.send( null );
let url = xmlHttp.responseText;


// notification part
let qr_img = "<h3>Scan this qr code with your phone :)<h3><br><img src=\"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chl=" +url+"\" >";
document.getElementById("alert_container").innerHTML = "<div class=\"alert alert-primary alert-dismissible fade show\" role=\"alert\">"+qr_img+
"<button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" aria-label=\"Close\"></button>"+
"</div>";
}


function get_private_ip(){
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", "http://127.0.0.1:21987/api/get_private_ip", false ); // false for synchronous request
Expand Down
39 changes: 6 additions & 33 deletions test.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,8 @@
from requests import get
from json import loads
from bs4 import BeautifulSoup
# keep main window hidden
from tkinter import Tk
from tkinter.filedialog import askopenfile

#isbn = "3302740068027"
#isbn = "978209157545"
isbn = "3270220060949"

def identify_product(isbn:str):



# edible product ?
r = get(f"http://world.openfoodfacts.org/api/v0/product/{isbn}")
r = loads(r.text)
if "product" in r.keys():
r = r["product"]

return {"name":r["product_name"]+ " - "+r["brands"] if "brands" in r.keys() else r["product_name"] ,"url":f"https://world.openfoodfacts.org/product/{isbn}"}


# book ?
r = get(f"https://www.isbnsearcher.com/books/{isbn}",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"})

if r.status_code == 200:
r = BeautifulSoup(r.text,"html.parser")

return {"name":r.find("h1").get_text(),"url":f"https://www.isbnsearcher.com/books/{isbn}"}

else:
return {"name":isbn,"url":f"https://www.google.com/search?q={isbn}"}

#print(identify_product(isbn))
# amazon lookup
r = get("https://www.amazon.fr/s?k=9782091575452",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"},allow_redirects=True)
Tk().withdraw()
# open file dialog
file_path = askopenfile(mode = "r")

0 comments on commit 1a25674

Please sign in to comment.