Uz Python Dev Logs
197 subscribers
57 photos
1 video
2 files
50 links
Uz Python Logs (notes)
Bu yerda Python dasturlash tiliga oid sharhlarni o'zbek tilida kuzatib borishingiz mumkin
👋 Bu yerda siz ham o'zingiz qiziqtirgan mavzuda qiziqarli ma'lumotlar e'lon qilishingiz mumkin
Aloqa: @AzamMamatmurodov
Ishlar: @uzpythonjobs
Download Telegram
Python'ning re moduli orqali matndan ma'lum shart asosida, matnning kerakli bo'lagini qirqib olish usulini - mover.uz'dagi video misolida ko'ramiz! Uni quyidagi funksiya orqali olish mumkin:


>>> url = 'https://mover.uz/watch/I2V2OKE/'
>>> import re
>>>
>>>
>>> def mover_video_url_getter(url):
... try:
... groups = re.search('https://mover.uz/watch/(.+?)/', url).groups()
... except Exception as e:
... print("Havola noto'g'ri berilgan")
... return
... else:
... if len(groups) > 0:
... video_name = groups[0]
... video_url = "https://v.mover.uz/{}_m.mp4".format(video_name)
... return video_url
... else:
... print('Mos havola topilmadi')
... return
...
>>> mover_video_url_getter(url)
'https://v.mover.uz/I2V2OKE_m.mp4'

Funksiyaning vazifasi: mover.uz'dagi biror videoga havolani berganda, funksiya video fayl turgan havolani qaytaradi.

@uzpythonlogs
#parsing #regex #videohavola
Python'da Mover.uz'dan berilgan havola orqali video'ni yuklab olish usuli:

import requests
import os


class Parse:
"""
kiruvchi havola: https://mover.uz/watch/PUxFuoqm/
chiquvchi video 360: https://v.mover.uz/PUxFuoqm_m.mp4
chiquvchi video 720: https://v.mover.uz/PUxFuoqm_h.mp4
"""
https = 'https://v.mover.uz/'
q_360 = '_m.mp4'
q_720 = '_h.mp4'
q = {
'360': q_360,
'720': q_720
}
user_agent = {'User-agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030529'}

def __init__(self, link):
self.link = link
self.quantity_list = []
self.movie_id = self.link.split('/')[-2] # PUxFuoqm

def search_movies(self):
quantity_360 = self.https + self.movie_id + self.q_360
quantity_720 = self.https + self.movie_id + self.q_720
self.processing_quantity(quantity_360, '360')
self.processing_quantity(quantity_720, '720')
return self.quantity_list

def processing_quantity(self, quantity, text):
res360 = requests.get(quantity, headers=self.user_agent)
if res360.status_code == 200:
self.quantity_list.append(text)

def uploads(self, list_uploads):
self.create_folder()
quantity = {
'360': self.https + self.movie_id + self.q_360,
'720': self.https + self.movie_id + self.q_720
}
for up in list_uploads:
res = requests.get(quantity[up], headers=self.user_agent)
with open(f"{os.getcwd()}/uploads/{up}_{quantity[up].split('/')[-1]}", 'wb') as f:
f.write(res.content)
return True

@staticmethod
def create_folder():
base_dir = os.getcwd()
list_dir = os.listdir(base_dir)
if 'uploads' not in list_dir:
try:
os.mkdir('uploads')
except OSError:
raise Exception("Uploads katalogini yaratib bo'lmadi, ruxsatni tekshiring")


if __name__ == "__main__":
parser = Parse('https://mover.uz/watch/PUxFuoqm/')
parser.uploads(parser.search_movies())

Rahmat: @alimanuz

@uzpythonlogs
#parsing #mover #videohavola #yuklabolish