Brasd99 commited on
Commit
094b3bd
1 Parent(s): 0696ff7

Refactoring for wrapper

Browse files
Files changed (1) hide show
  1. common/tinderwrapper.py +26 -25
common/tinderwrapper.py CHANGED
@@ -1,5 +1,4 @@
1
  import requests
2
- import json
3
 
4
  class TinderWrapper():
5
  def __init__(self, token):
@@ -28,37 +27,39 @@ class TinderWrapper():
28
 
29
  def get_updates(self, last_activity_date):
30
  try:
31
- url = self.host + '/updates'
32
- r = requests.post(url, headers=self.headers, data=json.dumps({"last_activity_date": last_activity_date}))
33
- print(r.json())
34
- return r.json()
 
35
  except requests.exceptions.RequestException as e:
36
- print(e)
37
 
38
  def get_photos(self, person):
39
- photos = person['photos']
40
- photo_urls = []
41
- for photo in photos:
42
- photo_urls.append(photo['url'])
43
- return photo_urls
44
 
45
  def create_dump(self, update_progress, max_percent=50, last_activity_date='1997-03-25T22:49:41.151Z'):
46
- update_progress(0, 'Делаю запрос в Tinder...')
47
  updates = self.get_updates(last_activity_date)['matches']
48
  update_progress(0, 'Обрабатываю ответ Tinder...')
 
49
  output = {}
 
 
50
  for i, update in enumerate(updates):
51
- person = update['person']
52
- photos = self.get_photos(person)
53
- person_id = person['_id']
54
- name = person['name']
55
- messages = update['messages']
56
- output[person_id] = {
57
- 'name': name,
58
- 'messages': messages,
59
- 'photos': photos
60
- }
61
- percent = ((i + 1) / len(updates)) * max_percent / 100
62
- update_progress(percent, 'Обрабатываю ответ Tinder...')
63
-
 
 
64
  return output
 
1
  import requests
 
2
 
3
  class TinderWrapper():
4
  def __init__(self, token):
 
27
 
28
  def get_updates(self, last_activity_date):
29
  try:
30
+ url = f"{self.host}/updates" # Using f-string for formatting
31
+ payload = {"last_activity_date": last_activity_date}
32
+ response = requests.post(url, headers=self.headers, json=payload) # Using json parameter instead of manually converting to JSON string
33
+ response.raise_for_status() # Raises an exception for 4xx or 5xx status codes
34
+ return response.json()
35
  except requests.exceptions.RequestException as e:
36
+ print(e)
37
 
38
  def get_photos(self, person):
39
+ return [photo['url'] for photo in person['photos']]
 
 
 
 
40
 
41
  def create_dump(self, update_progress, max_percent=50, last_activity_date='1997-03-25T22:49:41.151Z'):
42
+ update_progress(0, 'Выполняю запрос в Tinder...')
43
  updates = self.get_updates(last_activity_date)['matches']
44
  update_progress(0, 'Обрабатываю ответ Tinder...')
45
+
46
  output = {}
47
+ total_updates = len(updates)
48
+
49
  for i, update in enumerate(updates):
50
+ person = update['person']
51
+ photos = self.get_photos(person)
52
+ person_id = person['_id']
53
+ name = person['name']
54
+ messages = update['messages']
55
+
56
+ output[person_id] = {
57
+ 'name': name,
58
+ 'messages': messages,
59
+ 'photos': photos
60
+ }
61
+
62
+ percent = ((i + 1) / total_updates) * max_percent
63
+ update_progress(percent, 'Обрабатываю ответ Tinder...')
64
+
65
  return output