Python 在后台运行特定的Django脚本

Python 在后台运行特定的Django脚本,python,django,django-rest-framework,gmail-api,django-rest-auth,Python,Django,Django Rest Framework,Gmail Api,Django Rest Auth,我正在开发一个Gmail提取应用程序,并使用Gmail API从服务器获取邮件。问题在于,尽管我在后端框架中使用了线程,但邮件的获取时间太长。现在,我将实现一个功能,它将建议用户选择批量下载,“一旦您的下载准备就绪,我们将向您发送邮件”,但为此,我希望在后台的应用程序树中运行下面提到的download.py,一旦获取完成,它将被终止。 在代码的最底层,我想告诉用户他们的下载已经准备好了,但是它不起作用,尽管我已经在settings.py中定义了邮件服务器 下载.py import httplib

我正在开发一个Gmail提取应用程序,并使用Gmail API从服务器获取邮件。问题在于,尽管我在后端框架中使用了线程,但邮件的获取时间太长。现在,我将实现一个功能,它将建议用户选择批量下载,“一旦您的下载准备就绪,我们将向您发送邮件”,但为此,我希望在后台的应用程序树中运行下面提到的download.py,一旦获取完成,它将被终止。 在代码的最底层,我想告诉用户他们的下载已经准备好了,但是它不起作用,尽管我已经在settings.py中定义了邮件服务器

下载.py

import httplib2, base64
from stripogram import html2text
from oauth2client.django_orm import Storage
from apiclient.discovery import build
from oauth2client import client

from django.contrib.auth.models import User
from .models import CredentialsModel
from django.conf import settings

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import authentication, permissions
from gextracto import models
from gextracto.models import UserData
from django.core.mail import EmailMessage
from django.core import mail

connection = mail.get_connection()



class ListMails(APIView):
    """
    Gets a list of a specified number mail ids for a particular label
    Extracts the email in the form of plain/text
    The API returns all the extracted mails
    """

    authentication_classes = (authentication.SessionAuthentication,)
    permission_classes = (permissions.IsAuthenticated,)



    def extract_headers(self, message):
        """
        Extract the headers for a single mail and returns it
        {To, From, Subject}
        """

        needed_fields = ('From', 'To', 'Subject')
        return {i['name']:i['value'] for i in  message['payload']['headers'] if i['name'] in needed_fields}





    def get_message_body(self, message):
        """
        Get the body of an email
        Recursively look for the body for different mimetypes
        Returns the body as text/plain
        """

        if 'payload' in message:
            return self.get_message_body(message['payload'])

        elif 'parts' in message:
            return self.get_message_body(message['parts'][0])

        else:
            data = base64.urlsafe_b64decode(message['body']['data'].encode('ASCII'))
            markdown_data = html2text(data)#.decode('utf-8', "replace")
            data = data.replace("\n", "<br/>")

            # return {markdown, html}
            return {'markdown':unicode( markdown_data,"ISO-8859-1"), 'html':unicode(data,"ISO-8859-1")} if markdown_data else {'html':unicode(data,"ISO-8859-1")}




    def message_content_html(self, userId, message_id, service):
        """
        Make queries to get the content for a mail given its message id
        Returns all the content
        """

        content = {'id':message_id}
        # try
        message = service.users().messages().get(userId=userId, id=message_id).execute()

        mimetype = message['payload']['mimeType']

        if mimetype == 'text/html':
            return {}
        #
        else:
            body = self.get_message_body(message)
            if body == "":
                body = "<empty message>"

        headers = self.extract_headers(message)
        content['body'] = body
        content.update(headers)

        return content




    def collect_mails(self, user, messages, service):
        """
        Collect the content for all the mails currently downloaded
        """

        all_messages = []
        try:
            for message in messages:
                content = self.message_content_html(user.username, message['id'], service)
                if content:
                    all_messages.append(content)
            return all_messages

        # return empty list if no messages were downloaded
        except KeyError:
            return []


    def get(self, request, format=None):
        """
        Handles the GET request to get all the mails for a label
        Paginages through the GAPI content if required
        API returns all the messages
        {To, From, Subject, body}
        """

        user = request.user
        storage = Storage(CredentialsModel, 'id', user, 'credential')
        credentials = storage.get()
        http_auth = credentials.authorize(httplib2.Http())
        service = build('gmail', 'v1', http=http_auth)
        user_Id = user.username
        label_id = request.GET['label']

        # try
        # call Google API with a request to get a list of all the labels
        response = service.users().messages().list(userId=user_Id, labelIds=label_id, maxResults=100).execute()

        all_messages = self.collect_mails(user, response['messages'], service)


        if not all_messages:
            return Response([])


        else:
            if 'nextPageToken' in response:
                page_token_flag = True

            # request more more mails if the download limit has not yet been satisfied
                while(page_token_flag):
                    response = service.users().messages().list(userId=user_Id, pageToken=response['nextPageToken'], maxResults=100).execute()
                    all_messages.append(self.collect_mails(user, response['messages'], service))
                    print(all_messages)
                    #for x in range(0,len(all_messages)):
                    #b=all_messages[10]
                    #instance= UserData(user_id=user ,label=label_id, sender = b['From'] , subject=b['Subject'] , body=b['body'])
                    #instance.save()
                    page_token_flag = 'nextPageToken' in response

                    ##
            for x in range(0,len(all_messages)):
                b=all_messages[10]
                instance= UserData(user_id=user ,label=label_id, sender = b['From'] , subject=b['Subject'] , body=b['body'])
                instance.save()
            print ("Hi i am here!!!")    
            email = EmailMessage('Your Download Ready!', 'http://127.0.0.1:8000/admin/gextracto/userdata/', to=[user], connection=connection)
            email.send()
            connection.close()
        return Response(all_messages)
导入httplib2,base64
从条带图导入html2text
从oauth2client.django_orm导入存储
从apiclient.discovery导入生成
从oauth2client导入客户端
从django.contrib.auth.models导入用户
from.models导入凭证model
从django.conf导入设置
从rest_framework.views导入APIView
来自rest\u framework.response导入响应
从rest_框架导入身份验证、权限
从gextracto导入模型
从gextracto.models导入用户数据
从django.core.mail导入EmailMessage
从django.core导入邮件
connection=mail.get_connection()
类列表邮件(APIView):
"""
获取特定标签的指定数量邮件ID的列表
以纯文本形式提取电子邮件
API返回所有提取的邮件
"""
身份验证\类=(authentication.SessionAuthentication,)
权限\类=(permissions.IsAuthenticated,)
def extract_标题(自身、消息):
"""
提取单个邮件的标题并返回它
{To,From,Subject}
"""
必需的_字段=('From'、'To'、'Subject')
如果i['name']位于所需的_字段中,则在消息['payload']['headers']中为i返回{i['name']:i['value']
def get_消息体(自身、消息):
"""
获取电子邮件的正文
递归地查找不同mimetype的主体
以文本/纯文本形式返回正文
"""
如果消息中有“有效载荷”:
返回self.get_message_body(message['payload'])
elif消息中的“部件”:
返回self.get_message_body(message['parts'][0])
其他:
data=base64.urlsafe_b64解码(消息['body']['data'].encode('ASCII'))
markdown_data=html2text(数据)#.解码('utf-8',“替换”)
数据=数据。替换(“\n”和“
”) #返回{markdown,html} 返回{'markdown':unicode(markdown'u数据,“ISO-8859-1”),'html':unicode(数据,“ISO-8859-1”)}如果markdown'u数据不是{'html':unicode(数据,“ISO-8859-1”)} def message_content_html(self、userId、message_id、service): """ 根据邮件的邮件id进行查询以获取邮件的内容 返回所有内容 """ 内容={'id':消息\u id} #试一试 message=service.users().messages().get(userId=userId,id=message\u id).execute() mimetype=消息['payload']['mimetype'] 如果mimetype==“text/html”: 返回{} # 其他: body=self.get\u message\u body(消息) 如果body==“”: body=“” headers=self.extract_头(消息) 内容['body']=正文 content.update(标题) 返回内容 def收集电子邮件(自我、用户、消息、服务): """ 收集当前下载的所有邮件的内容 """ 所有_消息=[] 尝试: 对于消息中的消息: content=self.message\u content\u html(user.username,message['id'],service) 如果内容: 所有_消息。追加(内容) 返回所有消息 #如果未下载任何邮件,则返回空列表 除KeyError外: 返回[] def get(自我、请求、格式=无): """ 处理GET请求以获取标签的所有邮件 如果需要,通过GAPI内容分页 API返回所有消息 {To,From,Subject,body} """ user=request.user 存储=存储(CredentialsModel,'id',user,'credential') 凭据=存储。获取() http_auth=credentials.authorize(httplib2.http()) service=build('gmail','v1',http=http\u auth) user\u Id=user.username label\u id=request.GET['label'] #试一试 #通过请求调用GoogleAPI以获取所有标签的列表 response=service.users().messages().list(userId=user\u Id,labelIds=label\u Id,maxResults=100)。execute() all_messages=self.collect_邮件(用户、响应['messages],服务) 如果不是所有_消息: 返回响应([])) 其他: 如果响应为“nextPageToken”: 页面\u标记\u标志=真 #如果尚未满足下载限制,请请求更多邮件 while(页面标记): response=service.users().messages() 所有邮件.append(自我收集邮件(用户、响应['messages],服务)) 打印(所有信息) #对于范围内的x(0,len(所有_消息)): #b=所有_消息[10] #实例=用户数据(用户id=用户,标签=标签id,发件人=b['From'],主题=b['subject'],正文=b['body'])) #save()实例 page_token_flag='nextPageToken'响应 ## 对于范围内的x(0,len(所有_消息)): b=所有_消息[10] 实例=用户数据(用户id=用户,标签=标签id,发件人=b['From'],主题=b['subject