示例#1
0
文件: views.py 项目: duonghau/hoidap
 def post(self, request):
     form = QuestionForm(data=request.POST)
     if form.is_valid():
         question = Question(title=form.cleaned_data['title'],
                             content=form.cleaned_data['content'])
         question.author = request.user.profile
         question.save()
         for tagtitle in form.cleaned_data['tags']:
             try:
                 tag = Tag.objects.get(name=tagtitle)
             except ObjectDoesNotExist:
                 tag = Tag()
                 tag.name = tagtitle
                 tag.save()
             question.tags.add(tag)
             tag.save()
         question.author.rank += 0.01
         question.save()
         question.author.save()
         #notification
         users_notification = []
         for tag in question.tags.all():
             if tag.tag_followers.all().count() > 0:
                 for profile in tag.tag_followers.all():
                     if profile != request.user.profile and profile not in users_notification:
                         #create notification
                         notification = Notification()
                         notification.sender = request.user.profile
                         notification.recipient = profile
                         notification.action = 'addquestion'
                         notification.content_object = question
                         notification.save()
                         users_notification.append(profile)
         for follower in request.user.profile.followers.all():
             if follower not in users_notification:
                 notification = Notification()
                 notification.sender = request.user.profile
                 notification.recipient = follower
                 notification.action = 'addquestion'
                 notification.content_object = question
                 notification.save()
         return HttpResponseRedirect(
             reverse('question:detail', args=(question.pk, question.slug)))
     else:
         args = {}
         args['form'] = form
         args.update(csrf(request))
         return render(request, 'question_add.html', args)
示例#2
0
文件: views.py 项目: duonghau/hoidap
 def post(self,request):
     form=QuestionForm(data=request.POST)
     if form.is_valid():
         question=Question(title=form.cleaned_data['title'],content=form.cleaned_data['content'])
         question.author=request.user.profile
         question.save()
         for tagtitle in form.cleaned_data['tags']:
             try:
                 tag=Tag.objects.get(name=tagtitle)
             except ObjectDoesNotExist:
                 tag=Tag()
                 tag.name=tagtitle
                 tag.save()
             question.tags.add(tag)
             tag.save()
         question.author.rank+=0.01
         question.save()
         question.author.save()
         #notification
         users_notification=[]
         for tag in question.tags.all():
             if tag.tag_followers.all().count() > 0:
                 for profile in tag.tag_followers.all():
                     if profile!=request.user.profile and profile not in users_notification:
                         #create notification
                         notification=Notification()
                         notification.sender=request.user.profile
                         notification.recipient=profile
                         notification.action='addquestion'
                         notification.content_object=question
                         notification.save()
                         users_notification.append(profile)
         for follower in request.user.profile.followers.all():
             if follower not in users_notification:
                 notification=Notification()
                 notification.sender=request.user.profile
                 notification.recipient=follower
                 notification.action='addquestion'
                 notification.content_object=question
                 notification.save()
         return HttpResponseRedirect(reverse('question:detail',args=(question.pk, question.slug)))
     else:
         args={}
         args['form']=form
         args.update(csrf(request))
         return render(request,'question_add.html',args)
示例#3
0
def steam(request):

    Steam.objects.all().delete()
    Developer.objects.all().delete()
    Tag.objects.all().delete()
    Genre.objects.all().delete()

    pages = [
        2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
    ]
    pagebreak = 0
    for page in pages:
        if pagebreak == 3:
            break
        ++pagebreak
        session = requests.Session()
        session.headers = {
            "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"
        }
        url = 'https://store.steampowered.com/search/?os=win&filter=topsellers&page=' + format(
            page)

        content = session.get(url, verify=False).content

        soup = BeautifulSoup(content, "html.parser")
        topseller = soup.find('div', {'id': 'search_resultsRows'})
        steam = topseller.find_all('a')

        for i in steam:

            link = i['href']
            print(link)

            session = requests.Session()
            session.headers = {
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36"
            }
            url = link
            content = session.get(url, verify=False).content

            soup = BeautifulSoup(content, "html.parser")

            tags_list = list()

            try:
                metascore = soup.find('meta',
                                      {'itemprop': 'ratingValue'})['content']
                print(metascore)
                title = soup.find('div', {'class': 'apphub_AppName'}).text
                print(title)
                genre = soup.find('div', {'class': 'details_block'}).a.text
                print(genre)
                developer = soup.find('div', {'class': 'dev_row'}).a.text
                print(developer)
                price = soup.find('div', {'class': 'game_purchase_price'}).text
                newprice = price.strip()

                if newprice == 'Free to Play':
                    finalprice = 00.00

                else:
                    finalprice = (Decimal(newprice.strip('RM')))

                print(finalprice)

                image = soup.find('img',
                                  {'class': 'game_header_image_full'})['src']
                media_root = '/FYP/django/rise/media'
                if not image.startswith(("data:image", "javascript")):
                    #local_filename = image.split('/')[-1].split("?")[0]
                    local_filename = title + '.jpg'
                    r = session.get(image, stream=True, verify=False)
                    with open(local_filename, 'wb') as f:
                        for chunk in r.iter_content(chunk_size=1024):
                            f.write(chunk)

                    current_image_absolute_path = os.path.abspath(
                        local_filename)
                    shutil.move(current_image_absolute_path, media_root)

                tags = soup.find_all('a', {'class': 'app_tag'})

                for i in tags[0:5]:
                    tag = i.string.strip()
                    print(tag)

                    new_tag = Tag()
                    new_tag.name = tag
                    #new_tag.save()

                    if Tag.objects.filter(name=tag).count() < 1:
                        new_tag.save()

                    new_tag = Tag.objects.get(name=tag)

                    tags_list.append(new_tag)

                print(local_filename)

                new_genre = Genre()
                new_genre.name = genre
                #new_genre.save()

                if Genre.objects.filter(name=genre).count() < 1:
                    new_genre.save()

                new_genre = Genre.objects.get(name=genre)

                new_developer = Developer()
                new_developer.name = developer
                #new_developer.save()

                if Developer.objects.filter(name=developer).count() < 1:
                    new_developer.save()

                new_developer = Developer.objects.get(name=developer)

                new_steam = Steam()
                new_steam.gametitle = title
                new_steam.genre = new_genre
                new_steam.developer = new_developer
                new_steam.price = finalprice
                new_steam.url = link
                new_steam.rating = metascore
                new_steam.image = local_filename
                new_steam.save()

                for tag in tags_list:
                    new_steam.tags.add(tag)

                for row in Steam.objects.all():
                    if Steam.objects.filter(
                            gametitle=row.gametitle).count() > 1:
                        row.delete()

            except:
                pass

    context = {'steams': Steam.objects.all()}

    return render(request, 'steam/gamelist.html', context)
    """