Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. def writing_to_file(pack_of_news, pack_of_news_for_db, filename):
  2. """
  3. Writing information into 2 files: News_cache.txt and News_cache_json.json
  4. Creating 2 files because it's easier to read information into computer from JSON file than another file
  5. 1. Open file
  6. 2. Check if file is empty or not
  7. 3. If empty - append whole information
  8. If Not empty:
  9. 1. Check if the novelty in file
  10. 2. If in file: continue
  11. If Not in the file: Find out length of file (amount of news), put that number to incoming novelty
  12. If that novelty exists it will go to another novelty in limit
  13. It means that if you enter --limit 15 and 10 news are already in list it will add only 5 news to list
  14. """
  15. conn = sqlite3.connect("database.db")
  16. cursor = conn.cursor()
  17. cursor.execute('create table if not exists projects(num integer, title text, time text, source_link text, '
  18. 'description text, images_links text, alt_tx text, date_corrected integer, main_source text)')
  19. conn.commit()
  20. logging.info("Opening file News_cache.")
  21. with open(filename, 'a', encoding='utf-8') as news_cache:
  22. logging.info("Reading from News_cache.")
  23. content = reading_file('News_cache.txt')
  24. if not content:
  25. if cursor.execute("SELECT * FROM projects"):
  26. cursor.execute("DELETE FROM projects")
  27. conn.commit()
  28. for num, item in enumerate(pack_of_news):
  29. logging.info("Writing into file if it was empty.")
  30. news_cache.write(getting_info_into_file(item))
  31. news_cache.write("\n_ _ _")
  32. cursor.execute('insert into projects values (?,?,?,?,?,?,?,?,?)', pack_of_news_for_db[num])
  33. conn.commit()
  34. else:
  35. for number, item in enumerate(pack_of_news):
  36. if item.source_link in content:
  37. continue
  38. else:
  39. length = sum(1 for line in cursor.execute("SELECT * FROM projects")) + 1
  40. logging.info("Counting lines.")
  41. logging.info("Counted lines.")
  42. item.number_of_novelty = length
  43. logging.info("Writing into file if it was not empty.")
  44. news_cache.write(getting_info_into_file(item))
  45. news_cache.write("\n_ _ _")
  46. cursor.execute('insert into projects values (?,?,?,?,?,?,?,?,?)', pack_of_news_for_db[number])
  47. conn.commit()
  48. conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement