jonathanjordan21 commited on
Commit
d5681b3
1 Parent(s): b080a94

Update components/pexels.py

Browse files
Files changed (1) hide show
  1. components/pexels.py +14 -19
components/pexels.py CHANGED
@@ -2,7 +2,7 @@ import requests
2
  import shutil,os,re
3
 
4
  # Searching for the videos
5
- def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=1):
6
 
7
  if orientation not in ['potrait', 'landscape', 'square']:
8
  raise Exception("Error! orientation must be one of {'square', 'landscape', 'potrait'}")
@@ -30,27 +30,22 @@ def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoi
30
 
31
 
32
  # Video download function
33
- def download_video(data, parent_path, links, i):
34
- for x in data['videos'][0]['video_files'] :
35
-
36
- #if width != None and x['width'] < width:
37
- # continue
38
- #if height != None and x['height'] < height :
39
- #continue
40
- if x['link'] in links:
41
  continue
42
-
43
- vid = x
44
- print(vid['link'])
45
- with open(f"{os.path.join(parent_path,str(i) + '_' + str(vid['id']))}.mp4", 'bw') as f:
46
- f.write(requests.get(vid['link']).content)
47
- print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(vid['id'])) + '.mp4')
48
- return vid['link']
49
-
50
 
51
 
52
  # Utilizing the LLMs to find the relevant videos
53
- def generate_videos(product, api_key, orientation, llm_chain=None, sum_llm_chain=None):
54
  prod = product.strip().replace(" ", "_")
55
  links = []
56
  try :
@@ -74,7 +69,7 @@ def generate_videos(product, api_key, orientation, llm_chain=None, sum_llm_chain
74
  keyword = sum_llm_chain.run(s)
75
  print(i+1, ":", keyword)
76
  data = search_pexels(keyword, api_key, orientation.lower())
77
- link = download_video(data, prod, links,i)
78
  links.append(link)
79
 
80
  print("Success! videos has been generated")
 
2
  import shutil,os,re
3
 
4
  # Searching for the videos
5
+ def search_pexels(keyword, api_key, orientation='potrait', size='medium', endpoint='videos', num_pages=50):
6
 
7
  if orientation not in ['potrait', 'landscape', 'square']:
8
  raise Exception("Error! orientation must be one of {'square', 'landscape', 'potrait'}")
 
30
 
31
 
32
  # Video download function
33
+ def download_video(data, parent_path, height, width, links, i):
34
+ for x in data['videos'] :
35
+ if x['id'] in links:
 
 
 
 
 
36
  continue
37
+
38
+ vid = x['video_files']
39
+ for v in vid:
40
+ if v['height'] == height and v['width'] == width :
41
+ with open(f"{os.path.join(parent_path,str(i) + '_' + str(v['id']))}.mp4", 'bw') as f:
42
+ f.write(requests.get(v['link']).content)
43
+ print("Sucessfully saved video in", os.path.join(parent_path,str(i) + '_' + str(v['id'])) + '.mp4')
44
+ return x['id']
45
 
46
 
47
  # Utilizing the LLMs to find the relevant videos
48
+ def generate_videos(product, api_key, orientation, height, width, llm_chain=None, sum_llm_chain=None):
49
  prod = product.strip().replace(" ", "_")
50
  links = []
51
  try :
 
69
  keyword = sum_llm_chain.run(s)
70
  print(i+1, ":", keyword)
71
  data = search_pexels(keyword, api_key, orientation.lower())
72
+ link = download_video(data, prod, height, width, links,i)
73
  links.append(link)
74
 
75
  print("Success! videos has been generated")