blog.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from typing import List, Optional
  2. import requests
  3. import math
  4. from pyPodcastParser.Podcast import Podcast
  5. from dataclasses import dataclass
  6. from authors import Authors
  7. @dataclass
  8. class Post:
  9. id: int
  10. user_id: int
  11. title: str
  12. body: str
  13. audio_url: Optional[str] = None
  14. class Blog:
  15. def __init__(self, per_page: int = 10) -> None:
  16. self.posts = self.__get_posts()
  17. self.per_page = per_page
  18. self.authors = Authors()
  19. self.parse_posts()
  20. def __get_posts(self) -> List[Post]:
  21. return []
  22. def get_all(self) -> List[Post]:
  23. return self.posts
  24. def get_by_page(self, posts: list[Post] = None, page: int = 1) -> List[Post]:
  25. if posts is None: posts = self.posts
  26. start_post_index = (page - 1) * self.per_page
  27. end_post_index = page * self.per_page
  28. return posts[start_post_index:end_post_index]
  29. def get_pages_count(self, posts: list[Post] = None) -> List[int]:
  30. if posts is None: posts = self.posts
  31. return [i+1 for i in range(math.ceil(len(posts) / self.per_page))]
  32. def get_posts_by_user_id(self, user_id: int) -> List[Post]:
  33. posts = []
  34. for p in self.posts:
  35. if p.user_id == user_id:
  36. posts.append(p)
  37. return posts
  38. def get_post_by_id(self, post_id: int) -> Post | None:
  39. for p in self.posts:
  40. if p.id == post_id:
  41. return p
  42. return None
  43. def parse_posts(self):
  44. for l in ['https://soundcloud.com/bvgm', 'https://www.prahladanandaswami.com/index.php/feed/podcast/', 'https://bvks.ru/podcast/itunes/']:
  45. if l.split('/')[2] == 'soundcloud.com':
  46. l = get_feed_url_from_soundcloud(l)
  47. r = requests.get(l)
  48. podcast = Podcast(r.content)
  49. author = self.authors.get_user_by_name(podcast.itunes_author_name)
  50. if author.id == 666:
  51. author.id = self.authors.add_author(
  52. name=podcast.itunes_author_name,
  53. info=podcast.description,
  54. url=l
  55. )
  56. posts = []
  57. counter = len(self.posts)
  58. for a in podcast.items[::-1]:
  59. counter += 1
  60. posts.append(
  61. Post(
  62. id=counter,
  63. user_id=author.id,
  64. title=a.title,
  65. body=a.description,
  66. audio_url=a.enclosure_url
  67. )
  68. )
  69. self.posts = posts[::-1] + self.posts
  70. def add_post(self, post: Post) -> Post:
  71. self.posts.insert(0, post)
  72. print(self.posts[0])
  73. return post
  74. def get_feed_url_from_soundcloud(url: str) -> str:
  75. from bs4 import BeautifulSoup
  76. html = requests.get(url)
  77. soup = BeautifulSoup(html.content, 'html.parser')
  78. data = soup.find("meta", property="al:android:url")
  79. url = data['content']
  80. user_id = url.split(':')[-1]
  81. return f'http://feeds.soundcloud.com/users/soundcloud:users:{user_id}/sounds.rss'
  82. def get_fake_posts_data() -> list[Post]:
  83. r = requests.get('https://jsonplaceholder.typicode.com/posts')
  84. posts = r.json()
  85. posts.reverse()
  86. return [Post(
  87. id=p['id'],
  88. user_id=p['userId'],
  89. body=p['body'],
  90. title=p['title'].title()) for p in posts]