I have one web application with Rest API ,In that application I have some video files now i am creating one intermediate server in this one.I am accessing my web content using API but here i need to check updates in regular intervals if new updates will be there i need to download them.
Here files are video files and i'm using flask
I tried this i'm not getting
from flask import Flask,render_template,json,jsonify
import schedule
import time
import requests,json
from pathlib import Path
import multiprocessing
import time
import sys
import schedule,wget,requests,json,os,errno,shutil,time,config
def get_videos():
response = requests.get('my api here')
data = response.json()
files =list() # collecting my list of video files
l = len(data)
for i in range(l):
files.append(data[i]['filename'])
return files
def checkfor_file(myfiles):
for i in range(len(myfiles)):
url = 'http://website.com/static/Video/'+myfiles[i] # checking file exists are not in my folder
if url:
os.remove(url)
else:
pass
def get_newfiles(myfiles):
for i in range(len(myfiles)):
url = config.videos+myfiles[i]
filename= wget.download(url)# downloading video files here
def move_files(myfiles):
for i in range(len(myfiles)):
file = myfiles[i]
shutil.move(config.source_files+file,config.destinatin) # moving download folder to another folder
def videos():
files = set(get_videos()) # getting only unique file only
myfiles = list(files)
checkfor_file(myfiles)
get_newfiles(myfiles)
move_files(myfiles)
def job():
videos()
schedule.every(10).minutes.do(job) # running for every ten minutes
while True:
schedule.run_pending()
time.sleep(1)
pi =Flask(__name__)
#pi.route('/')
def index():
response = requests.get('myapi')
data = response.json()
return render_template('main.html',data=data)
Related
I would like to realize the following with twisted web:
The user clicks on a link on a page
This is bring the user to a temporary page where the user is informed that the PDF is generated
When the PDF generation is finished, the download starts automatically.
This is my example code:
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web.server import NOT_DONE_YET
class DownloadResource(Resource):
isLeaf = True
def render_GET(self, request: Resource):
# Start generating the PDF file in the background
reactor.callInThread(generate_pdf, request)
request.write(b"<html><body>Please wait while the PDF file is generated and download will start soon...</body></html>")
return NOT_DONE_YET
def generate_pdf(request: Resource):
# Generate the PDF data
# for the time being, just open it from the disk.
# the real application will generate it.
filename = r'/path/to/pdf'
with open(filename, 'rb') as f:
pdf_data = f.read()
# Serve the PDF data
request.setHeader(b"content-type", b"application/pdf")
request.setHeader(b"content-length", str(len(pdf_data)).encode('utf-8'))
request.write(pdf_data)
request.finish()
resource = DownloadResource()
This works almost ok, the point is that the pdf data are written as bytes next to the text message. Something like this:
Please wait while the PDF file is generated and download will start soon...%PDF-1.4 %ª«¬ 1 0 obj << /Title....
Instead I would like the pdf file to be downloaded. Ideally I would need a kind of send_file. Does it exist?
Can you help me in solving this issue?
I created an automated script in Python to change my wallpaper on my desktop using the unsplash.com API. It runs fine, except when I tell it to save the file as wallpaper.jpg it does that once, then every file saved thereafter is saved as wallpaper(1).jpg. Why is this happening? I want the file to be saved simply as wallpaper.jpg.
I worked around the problem in the change_wallpaper function so that it retrieves the wallpaper(1).jpg file, but I would rather not have to do this if possible.
# import dependencies
import os
import requests
import wget
import ctypes
import time
from config import UNSPLASH_ACCESS_KEY
def get_wallpaper():
# create API query and define parameters
url = 'https://api.unsplash.com/photos/random?client_id=' + UNSPLASH_ACCESS_KEY
params = {
"query": "HD Wallpapers",
"orientation": "landscape"
}
# create request, define json format, grab image url, and save to folder
response = requests.get(url, params=params).json()
image_url = response['urls']['full']
# download the image
image = wget.download(image_url, 'tmp/wallpaper.jpg')
return image
def change_wallpaper():
get_wallpaper()
# create path using cwd, current working directory, and use ctypes to update the designated image as the wallpaper.
path = os.getcwd()+'\\tmp\\wallpaper (1).jpg'
ctypes.windll.user32.SystemParametersInfoW(20,0, path,3)
def main():
try:
while True:
change_wallpaper()
time.sleep(10)
except KeyboardInterrupt:
print("\nThank you, come again!")
except Exception as e:
pass
if __name__ == "__main__":
main()
If the wget module doesn't have the capability to overwrite existing files you will need to delete the existing file before you try to download a new one. You can do this within your code as follows.
import os
if os.path.exists(filename):
os.remove(filename)
I am trying to change my code to support video processing from multiple sites (youtube, vimeo, etc.) using the youtube extractions. I don't want to import youtube-dl (unless necessary). I would prefer to call a function. my understanding is that this: youtube-dl http://vimeo.com/channels/YOUR-CHANNEL) is a command line tool. please help!
import pymongo
import get_media
import configparser as ConfigParser
# shorten list to first 10 items
def shorten_list(mylist):
return mylist[:10]
def main():
config = ConfigParser.ConfigParser()
config.read('settings.cfg')
youtubedl_filename = config.get('media', 'youtubedl_input')
print('creating file: %s - to be used as input for youtubedl' % youtubedl_filename)
db = get_media.connect_to_media_db()
items = db.raw
url_list = []
cursor = items.find()
records = dict((record['_id'], record) for record in cursor)
# iterate through records in media items collection
# if 'Url' field exists and starts with youtube, add url to list
for item in records:
item_dict = records[item]
#print(item_dict)
if 'Url' in item_dict['Data']:
url = item_dict['Data']['Url']
if url.startswith('https://www.youtube.com/'):
url_list.append(url)
# for testing purposes
# shorten list to only download a few files at a time
url_list = shorten_list(url_list)
# save list of youtube media file urls
with open(youtubedl_filename, 'w') as f:
for url in url_list:
f.write(url+'\n')
if __name__ == "__main__":
main()
I'm trying to recursively download all of the directories and files of a website starting from its root. I tried writing the code for this, but I am unsure as to how I am supposed to get Python to create the appropriate directories and files. I understand how I can use requests to make GET requests to sites, but I don't know how I can actually write the files needed to my system. Here's my code:
import os
import requests
import getpass
class Fetch:
url=''
def __init__(self, url):
self.url = url
user = getpass.getuser()
os.chdir('/home' + user)
def download():
r = requests.get(url, stream=True)
I'm developing an application that runs on an Apache server with Django framework. My current script works fine when it runs on the local desktop (without Django). The script downloads all the images from a website to a folder on the desktop. However, when I run the script on the server a file object is just create by Django that apparently has something in it (should be google's logo), however, I can't open up the file. I also create an html file, updated image link locations, but the html file gets created fine, I'm assuming because it's all text, maybe? I believe I may have to use a file wrapper somewhere, but I'm not sure. Any help is appreciated, below is my code, Thanks!
from django.http import HttpResponse
from bs4 import BeautifulSoup as bsoup
import urlparse
from urllib2 import urlopen
from urllib import urlretrieve
import os
import sys
import zipfile
from django.core.servers.basehttp import FileWrapper
def getdata(request):
out = 'C:\Users\user\Desktop\images'
if request.GET.get('q'):
#url = str(request.GET['q'])
url = "http://google.com"
soup = bsoup(urlopen(url))
parsedURL = list(urlparse.urlparse(url))
for image in soup.findAll("img"):
print "Old Image Path: %(src)s" % image
#Get file name
filename = image["src"].split("/")[-1]
#Get full path name if url has to be parsed
parsedURL[2] = image["src"]
image["src"] = '%s\%s' % (out,filename)
print 'New Path: %s' % image["src"]
# print image
outpath = os.path.join(out, filename)
#retrieve images
if image["src"].lower().startswith("http"):
urlretrieve(image["src"], outpath)
else:
urlretrieve(urlparse.urlunparse(parsedURL), out) #Constructs URL from tuple (parsedURL)
#Create HTML File and writes to it to check output (stored in same directory).
html = soup.prettify("utf-8")
with open("output.html", "wb") as file:
file.write(html)
else:
url = 'You submitted nothing!'
return HttpResponse(url)
My problem had to do with storing the files on the desktop. I stored the files in the DJango workspace folder, changed the paths, and it worked for me.