You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

218 line
8.4 KiB

  1. # This script is intended for personal and scientific use only
  2. import os
  3. import sys
  4. import re
  5. import hashlib
  6. import json
  7. from time import sleep
  8. import feedparser
  9. import requests
  10. from bs4 import BeautifulSoup, Comment
  11. from bs4.element import CData
  12. # default config location is a 'config.json' next to the script.
  13. try:
  14. filedir = os.path.dirname(os.path.abspath(__file__))
  15. if len(sys.argv) < 2:
  16. configpath = filedir+'/config.json'
  17. print("Using default config location: ", configpath)
  18. config = json.load(open(configpath))
  19. else:
  20. configpath = sys.argv[1]
  21. config = json.load(open(configpath))
  22. except:
  23. print("Problem reading config file: ", configpath)
  24. print("ERROR: Config file not found or invalid!")
  25. sys.exit(1)
  26. print(filedir)
  27. public_path = filedir + '/public'
  28. assets_path = public_path + '/assets'
  29. feeds_path = public_path + '/feeds'
  30. # e.g. https://example.com/some-string
  31. assets_url = config['assets_url']
  32. # "I'm a robot which promises you clicks and $ ... Give me ALL your content!"
  33. requestheaders = {
  34. 'user-'+'age'+'nt' :
  35. 'Mo' + 'zill' + 'a/5.' + '0 (' + 'comp' + 'ati' + 'ble; '+'Go'
  36. + 'og'+'le'+ 'bo' + 't/' + '2.1; +http' + '://www.' + 'go'
  37. + 'og'+ 'le'+'.com/'+'bo'+'t.html)'
  38. }
  39. # need filname safe strings for storing images along html files
  40. def get_valid_filename(s):
  41. s = str(s).split('?')[0].strip().strip('/').strip('http://').strip('https://').replace(' ', '-')
  42. return re.sub(r'(?u)[^-\w.]', '-', s)
  43. # Get a unique and valid filename from URL (for images)
  44. def filename_from_url(url):
  45. # remove get attributes and path
  46. new_filename = url.split('?')[0].split('/')[-1]
  47. # Split filename
  48. new_filename = new_filename.split('.')
  49. # insert a hash before suffix
  50. new_filename.insert(1, str(hashlib.md5(url.encode('utf-8')).hexdigest()) )
  51. # convert back to string and extra validate
  52. new_filename = get_valid_filename('.'.join(new_filename))
  53. return new_filename
  54. # Download images and so on
  55. def download_image(url, entry_dir, filename):
  56. # take care of protocol relative URLs ... let's just assume that https works.
  57. if url.startswith('//'):
  58. url = 'https:'+url
  59. response = requests.get(url, headers=requestheaders)
  60. if response.status_code == 200:
  61. with open(assets_path + '/' + entry_dir + '/' + filename, 'wb') as f:
  62. #f.write(response.content)
  63. for chunk in response.iter_content(1024):
  64. f.write(chunk)
  65. def process_feed(feed_url, output_filename):
  66. # Get the feed
  67. r_feed = requests.get(feed_url, headers=requestheaders)
  68. # TODO: exceptions.(what if 404 or whatever?)
  69. # Store data of new articles
  70. for entry in feedparser.parse(r_feed.text).entries:
  71. print(entry.link)
  72. entry_dir = get_valid_filename(entry.link) # input e.g. https://orf.at/stories/3117136/
  73. entry_path = assets_path + '/'+ entry_dir
  74. if not os.path.exists(entry_path):
  75. r = requests.get(entry.link.split('?')[0], headers=requestheaders)
  76. online_soup = BeautifulSoup(r.text, 'html.parser')
  77. content_soup = BeautifulSoup('<article></article>', 'html.parser')
  78. # Remove all Comments
  79. for element in online_soup(text=lambda text: isinstance(text, Comment)):
  80. element.extract()
  81. # domain and path specific rules
  82. # ... split strings for (very simple) ob+fu+sca+tion
  83. if entry.link.startswith('https://or'+'f.a'+'t/sto'+'ries'):
  84. if entry.date:
  85. article_time = content_soup.new_tag('time', datetime=entry.date)
  86. content_soup.article.append(article_time)
  87. article_headline = online_soup.find('h1', attrs={'class': 'story-lead-headline'})
  88. content_soup.article.append(article_headline)
  89. article_body = online_soup.find('div', attrs={'class': 'story-content'})
  90. content_soup.article.append(article_body)
  91. article_link = content_soup.new_tag('a', href=entry.link)
  92. article_link['class'] = 'source';
  93. article_link.string = 'Quelle (' + entry.link + ')'
  94. content_soup.article.append(article_link)
  95. if entry.link.startswith('https://de'+'rst'+'and'+'ard'+'.a'+'t/20'): # url starts with number ... too lazy for regex :)
  96. if entry.published:
  97. article_time = content_soup.new_tag('time', datetime=entry.published)
  98. content_soup.article.append(article_time)
  99. article_headline = online_soup.find('h1', attrs={'itemprop': 'headline'})
  100. content_soup.article.append(article_headline)
  101. # images etc
  102. article_aside = online_soup.find('div', id="content-aside")
  103. content_soup.article.append(article_aside)
  104. article_body = online_soup.find('div', attrs={'itemprop': 'articleBody'})
  105. content_soup.article.append(article_body)
  106. article_link = content_soup.new_tag('a', href=entry.link)
  107. article_link['class'] = 'source';
  108. article_link.string = 'Quelle (' + entry.link.split('?')[0] + ')'
  109. content_soup.article.append(article_link)
  110. # modify original link -> mobile version and comment section
  111. link_to_comments = re.sub(r'(\/\/)', r'\1mobil.',entry.link.split('?')[0]) + '?_viewMode=forum#'
  112. article_comments_link = content_soup.new_tag('a', href=link_to_comments)
  113. article_comments_link['class'] = 'comments';
  114. article_comments_link.sting = 'Kommentare'
  115. content_soup.article.append(article_comments_link)
  116. article_link.string = 'Quelle (' + entry.link.split('?')[0] + ')'
  117. content_soup.article.append(article_link)
  118. # create directory for storing and serving html and images
  119. os.makedirs(entry_path)
  120. # download all article images and replace image source
  121. for img in content_soup.findAll('img'):
  122. print(img)
  123. if img.get('data-src'):
  124. old_url = img['data-src']
  125. if not old_url.startswith('data:'):
  126. new_filename = filename_from_url(old_url)
  127. img['data-src'] = assets_url + '/' + entry_dir + '/' + new_filename
  128. download_image(old_url, entry_dir, new_filename)
  129. if img.get('src'):
  130. old_url = img['src']
  131. if not old_url.startswith('data:'):
  132. new_filename = filename_from_url(old_url)
  133. img['src'] = assets_url + '/' + entry_dir + '/' + new_filename
  134. download_image(old_url, entry_dir, new_filename)
  135. if img.get('data-srcset'):
  136. srcset = img['data-srcset'].split(', ')
  137. new_srcset = []
  138. for src in srcset:
  139. old_url = src.split(' ')[0]
  140. src_res = src.split(' ')[1]
  141. new_filename = filename_from_url(old_url)
  142. download_image(old_url, entry_dir, new_filename)
  143. new_url = assets_url + '/' + entry_dir + '/' + new_filename
  144. src = ' '.join([new_url, src_res])
  145. new_srcset.append(src)
  146. img['data-srcset'] = ', '.join(new_srcset)
  147. # TODO(?): HTML5 picture tag
  148. f = open(entry_path + '/index.html', 'w')
  149. f.write(str(content_soup))
  150. f.close()
  151. sleep(1.3)
  152. # Create new feed
  153. # Maybe buiding a new feed from scretch using a template would be nicer but ...
  154. # let's just modify the original one!
  155. feed_soup = BeautifulSoup(r_feed.text, 'lxml-xml')
  156. for e in feed_soup.findAll('item'):
  157. entry_dir = get_valid_filename(e.link.text)
  158. f_content = open(assets_path + '/' + entry_dir + '/index.html', 'r')
  159. content_tag = feed_soup.new_tag('content:encoded')
  160. content_tag.string = CData(f_content.read())
  161. e.append(content_tag)
  162. f_content.close
  163. # create directory if not present
  164. os.makedirs(feeds_path, exist_ok=True)
  165. f = open(feeds_path + '/' + output_filename, 'w')
  166. f.write(str(feed_soup.prettify()))
  167. f.close()
  168. # Let's actually fetch the stuff!
  169. for feed in config['feeds']:
  170. process_feed(feed['source'], feed['destination'])